-2

Here the question is if the behaviour of some compilers is correct and standard.

My understanding of the reinterpret_cast ( I may be wrong ) is that when A a;B b; then reinterpret_cast<A>b is equivalent to *((A*)&b).

According to that vision the code bellow should work but in g++ it doesn't is it standard ?

struct A{
    int a;
};

int main(int argc, char **argv)
{
  struct A x;
  x.a=5;
  int b=*reinterpret_cast<int*>(&x);
// the previous works but it should work as bellow
  int b=reinterpret_cast<int>(x);
  return 0;
}
George Kourtis
  • 2,381
  • 3
  • 18
  • 28
  • Is that really what you are trying to compile? – juanchopanza Aug 03 '14 at 20:55
  • That doesn't even compile, because that's not how you use reinterpret_cast and you can't use return; in a statement that epects a return-value. – Shokwav Aug 03 '14 at 20:57
  • 2
    reinterpret_cast ??? should be int b = reinterpret_cast(x) or better: reinterpret_cast(x) – firda Aug 03 '14 at 20:57
  • When you do get it to compile, remember it's probably wrong! reinterpret_cast is bad news 99% of the time – Neil Kirk Aug 03 '14 at 20:58
  • reiterpret_cast(variable) that is how templates work between the < > u put the type – Lefsler Aug 03 '14 at 20:59
  • Sorry for the errors, I should check compilation before posting. – George Kourtis Aug 03 '14 at 21:01
  • @GeorgeKourtis It still doesn't compile. – Neil Kirk Aug 03 '14 at 21:03
  • @zyboxinternational Why on earth would one `return NULL` if `main()` returns an `int`? – juanchopanza Aug 03 '14 at 21:09
  • @juanchopanza I was giving a basic example that the `return 0;` is basically not returning anything of use. – AStopher Aug 03 '14 at 21:12
  • Why `return 0;` anyway, there's the exact same as `return;`. I can't see what the OP is trying to get out of the `return 0;`. If you're not returning anything of use, then to my understanding the function should be `void` (well, it would if it wasn't the `main` function). – AStopher Aug 03 '14 at 21:14
  • @zyboxinternational Come on, `return;` would be illegal. I realise now you don't know what you are talking about, so I will ignore your comments. – juanchopanza Aug 03 '14 at 21:16
  • @juanchopanza I was making an example, not that it had to be taken literally. You're slightly on the rude side- you've got tons more rep than me, you know the rules.. – AStopher Aug 03 '14 at 21:46

2 Answers2

0

reinterpret_cast<A>(b) is not legal if A and B are unrelated types (though I vaguely recall that some earlier versions of MSVC might have allowed it). What you're probably looking for is reinterpret_cast<A const&>(b), which likewise reinterprets the bits of b as belonging to an A. (Or reinterpret_cast<A&>, if you want to write to it -- the main point is the reference.)

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • Isn't reinterpret_cast well-behaved if both A and B have the exact same layout? I know this is true in C, but I'm not sure if the same applies to C++. – Shokwav Aug 03 '14 at 20:58
  • You'd have to define "well-behaved". I honestly don't know whether it compiles -- I'd use a reference for that too. Assuming it does, it doesn't invoke undefined behavior (just like in C). – Sneftel Aug 03 '14 at 20:59
  • Shokwav: it is legal under certain conditions. For instance, it is legal to `reinterpet_cast` a pointer to an object of a standard-layout class to a pointer of the first non-static data member. – peppe Aug 03 '14 at 21:05
  • @peppe It's certainly legal to do it to the pointer. I think Shokwav's point was whether it was legal to cast a non-pointer non-reference if the underlying layout was the same. – Sneftel Aug 03 '14 at 21:09
  • According to http://en.cppreference.com/w/cpp/language/reinterpret_cast "Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type." That using my brain translates exactly to (*&x) So I am expecting from reinterpret_cast exactly that. WHY NOT ? – George Kourtis Aug 03 '14 at 21:12
0

I assume that you're trying to do something like this:

struct A{int a};

int main(){
    A x;
    x.a = 5;

    int* b = reinterpret_cast<int*>(&x);
}

In which case the answer is no, this is not standard; it is completely UB. Also, in the case of pointers, it also violates strict aliasing (What is the strict aliasing rule?).

Community
  • 1
  • 1
Shokwav
  • 654
  • 8
  • 19
  • Yes instead of using what I am thinking should be the "normal" way of doing it: reinterpret_castx; I am obliged to do int b =*(reinterpret_cast(&x)) and I don't like doing it. So what is reinterpret cast used for ? – George Kourtis Aug 03 '14 at 21:10
  • @GeorgeKourtis: reinterpret_cast is when you need to break the rules of the language in order to do something very (platform) specific. It has very few legitimate uses; in fact, the only use case I've ran across was a delegate implementation that used it specifically for handling MSVC's nonstandard behavior. In short: do not use it, unless you REALLY know what you're doing. – Shokwav Aug 03 '14 at 21:13
  • Hmm? AFAICT, the usage you posted here is totally legal. For POD types, the rules for casting and aliasing go by what the underlying basic type is, not what user-defined struct it's in. – Sneftel Aug 04 '14 at 07:14