This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C++ reinterpret_cast between pointers and integral types


"Kaveh R. Ghazi" wrote:

BTW, enumeration types are not integral types. bool is an integral
type [3.9.1]/7 (just in case anyone was confused).

reinterpret_cast[5.2.10] lists the conversions that can be done,
and no others are permitted.

'a value of integral or enumeration type ... to pointer', so all these
ones are valid.
>   void *p1 = reinterpret_cast<void*>(~0LL);
>   void *p2 = reinterpret_cast<void*>(~0);
>   void *p3 = reinterpret_cast<void*>(~(enum e)0);
>   void *p4 = reinterpret_cast<void*>(false);

'a pointer can be converted to any integral type large enough to hold
it ...'.  However, we do allow loss of precision with a ped_warn,
so we should allow ptr->bool likewise.

>   int i1 = reinterpret_cast<int>((void*)0);
>   char c1 = reinterpret_cast<char>((void*)0);
>   bool b1 = reinterpret_cast<bool>((void*)0);

There is nothing to support the following being legal.
>   enum e e1 = reinterpret_cast<enum e>((void*)0);

> Should the first clause warn about lost precision too?
> E.g. p1 on 32bit pointer targets.
No. [5.2.10]/5 says you can convert a pointer 'to integral type of
sufficient size' and back again without loss. If we warn for loss
of precision on both int->ptr and ptr->int, it becomes annoying.

> If the answers are yes, I can send a patch.  Otherwise, I'd like to
> add comments to the code explaining why.

hope the above helps.

nathan

-- 
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]