迭代器,指针和(T *) - 1 [英] iterators, pointers and (T*)-1

查看:81
本文介绍了迭代器,指针和(T *) - 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在考虑使用指针和迭代器来模板化一些代码。


指针有3种值。


- Null

- 无效(值为((T *) - 1))

- 有效

Null表示指针为空 - 很酷。


无效表示指针未初始化 - 使用此值

失败断言。


对象的有效点。

现在我想使用迭代器,我无法访问容器。


我以为我可以使用:


typedef map< X> :: iterator container_iterator; //任何容器


union Ptr

{

ptr_diff_t val;

container_iterator ptr;

};

bool IsNull(const Ptr& ptr)

{

断言(ptr.val!= -1);

返回(ptr.val == 0);

}


container_iterator :: value_type& dereference(Ptr& ptr)

{

断言(ptr.val!= -1);

return * ptr.ptr; < br $>
}


我是否打破了某些事情?我可能做到了。首先,一个container_iterator

可能占用比ptr_diff_t更多的空间,即使它没有,这里的

也可能不是普遍真实的。


问题是,我是否遗漏了关于迭代器的事情?

是一种将STL迭代器初始化为null的方法吗?并且还没有创建比迭代器本身大的类的无效

值?


如果你能包含它我会发现它非常有用其他有趣的
迭代器的
值。

POD指针的示例很简单。这段代码适用于符合C ++编译器的任何符合
的内容。

typedef value_type * pointer_type;


struct Ptr < br $>
{

pointer_type ptr;

};


bool IsNull(const Ptr& ptr)

{

断言(ptr.ptr!=(pointer_type *)-1);

return(ptr.val == 0);

}


container_iterator :: value_type& dereference(Ptr& ptr)

{

assert(ptr.ptr!=(pointer_type *)-1);

return * ptr .ptr;

}

解决方案



" ; Gianni Mariani < GI ******* @ mariani.ws>在消息新闻中写道:bn ******** @ dispatch.concentric.net ...

- 无效(值为((T *) - 1))




不保证你可以将-1转换为指针类型并获取值。


Ron Natalie写道:

" Gianni Mariani" < GI ******* @ mariani.ws>在消息新闻中写道:bn ******** @ dispatch.concentric.net ...

- 无效(值为((T *) -1))



不保证你可以将-1转换为指针类型并返回值。




是的,但这不是它正在做的事情。


假设是:


((T *) - 1)==((T *) - 1)


在((T *) - 1)的某些不太可能的情况下可能出现问题

机会一个有效的指针 - 我不会在那上面睡不着觉。




" Gianni马里亚尼" < GI ******* @ mariani.ws>在消息中写道

news:bn ******** @ dispatch.concentric.net ...

Ron Natalie写道:

不保证你可以将-1转换为指针类型并获得值

是的,但这不是它正在做的事情。

假设是:

((T *) - 1)==((T *) - 1)

可能会出现问题在不太可能发生的情况下((T *) - 1)有可能是一个有效的指针 - 我不会因此而失眠。




更可能的结果是编译器拒绝编译你的程序

直接。


实现不需要接受非零的程序

指针的积分值。



I''m looking at templatizing some code that uses pointers and iterators.

There are 3 kinds of values for pointers.

- Null
- Invalid ( the value is ((T*)-1) )
- Valid
Null indicates that the pointer is Null - cool.

Invalid indicates that the pointer is uninitialized - using this value
fails asserts.

Valid points to an object.
Now I want to use iterators and I don''t have access to a container.

I was thinking I could use:

typedef map<X>::iterator container_iterator; // any container

union Ptr
{
ptr_diff_t val;
container_iterator ptr;
};
bool IsNull( const Ptr & ptr )
{
assert( ptr.val != -1 );
return ( ptr.val == 0 );
}

container_iterator::value_type & dereference ( Ptr & ptr )
{
assert( ptr.val != -1 );
return * ptr.ptr;
}

Did I break somthing ? I probably did. Firstly, a container_iterator
may occupy more space than a ptr_diff_t and even if it did not, the are
assuptions here that are probably not universally true.

The question is, am I missing somthing regarding iterators ? Is there
a way to initialize an STL iterator to a "null" and also an "invalid"
value without creating a class that is larger than the iterator itself ?

I would find it very useful if you could contain other interesting
values for iterators.
The example for a POD pointer is easy. This code should work on any
conforming C++ compiler.

typedef value_type * pointer_type;

struct Ptr
{
pointer_type ptr;
};

bool IsNull( const Ptr & ptr )
{
assert( ptr.ptr != ( pointer_type * ) -1);
return ( ptr.val == 0 );
}

container_iterator::value_type & dereference ( Ptr & ptr )
{
assert( ptr.ptr != ( pointer_type * ) -1);
return * ptr.ptr;
}


解决方案


"Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bn********@dispatch.concentric.net...

- Invalid ( the value is ((T*)-1) )



No guarantee that you can cast -1 to a pointer type and get the value back.


Ron Natalie wrote:

"Gianni Mariani" <gi*******@mariani.ws> wrote in message news:bn********@dispatch.concentric.net...

- Invalid ( the value is ((T*)-1) )


No guarantee that you can cast -1 to a pointer type and get the value back.



True, but that''s not what it''s doing.

The assumption is:

((T*)-1) == ((T*)-1)

A problem may arise in the unlikely event that ((T*)-1) is by some
chance a valid pointer - I won''t loose sleep on that.



"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bn********@dispatch.concentric.net...

Ron Natalie wrote:

No guarantee that you can cast -1 to a pointer type and get the value

back.
True, but that''s not what it''s doing.

The assumption is:

((T*)-1) == ((T*)-1)

A problem may arise in the unlikely event that ((T*)-1) is by some
chance a valid pointer - I won''t loose sleep on that.



A more likely outcome is the compiler refusing to compile your program
outright.

Implementations are not required to accept programs that cast nonzero
integral values to pointers.


这篇关于迭代器,指针和(T *) - 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆