[cfe-dev] Preliminary reinterpret_cast Sema patch

Argiris Kirtzidis akyrtzi at gmail.com
Sun Oct 19 04:48:23 PDT 2008

Hi Sebastian,

Some comments about the tests:

>   // G++ doesn't like the first two. I disagree with that. /2 allows 
> casting
>   // an expression to its own type, subject to all other restrictions. 
> The only
>   // restriction mentioned, however, is that casting away constness is 
> invalid.
>   // It should therefore be valid to cast a non-pointer type to its 
> own type,
>   // no matter how useless that is.
>   int i = 0;
>   i = reinterpret_cast<int>(i);
>   structure s;
>   s = reinterpret_cast<structure>(s);

These are errors for MSVC and Comeau too. I think the "Subject to the 
restrictions in this section" refers to the whole 5.2.10 section, not 
the paragraph.
See here for another related perspective:

>   float *fp = reinterpret_cast<float*>(l);
>   // Note: should fail on 64-bit, but LLVM is target-agnostic. Is it 
> possible
>   // to annotate the bytecode to make this fail in native codegen?
>   int i = reinterpret_cast<int>(fp);

LLVM IR produced by C/C++ cannot be target-agnostic. See: 
You can use TargetInfo to check whether the above conversion is legal.

>   fnptr2 fp2 = reinterpret_cast<fnptr2>(fp);
>   // Should fail in C++03 without extensions, succeed with exts or in 
> 0x mode.
>   void *vp = reinterpret_cast<void*>(fp2);

All three compilers accept this; I think Clang should accept it too, 
programmers will be unpleasantly surprised if they find this is not allowed.


Sebastian Redl wrote:
> Hi,
> For people who want to write test cases, or otherwise play with this, 
> here's my preliminary patch for semantic analysis of reinterpret_cast. 
> The patch also includes my previous work on const_cast, slightly updated.
> The patch doesn't include reinterpret_cast test cases yet, but I've 
> attached another file containing a number of commented (but not 
> -verify-ready) test cases I wrote last night.
> Sebastian
> ------------------------------------------------------------------------
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

More information about the cfe-dev mailing list