Jump to content
  • Advertisement
Sign in to follow this  
mrmrcoleman

Is this a legal use of a reference in C++?

This topic is 4626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Ask your compiler ;-)

Since a reference is just another name for your variable, it has to be of the same type.

If you want to convert a double to binary representation and vice versa you can use an union.

Share this post


Link to post
Share on other sites
Quote:
Original post by nmi
If you want to convert a double to binary representation and vice versa you can use an union.

Although this works in all compilers that I know of I believe it is non-standard. The C++ Final Draft Standard is not overly clear on the matter but does state:
Quote:
C++ Standard, Final Draft, Section 9.5, Paragraph 1
In a union, at most one of the data members can be active at any time, that is, the value of at most one of the data members can be stored in a union at any time...

The final draft C99 standard is clearer:
Quote:
C99 Standard, Final Draft, Section 6.5.2.3, Paragraph 5
With one exception, if the value of a member of a union object is used when the most recent store to the object was to a different member, the behavior is
implementation-defined...

I believe the same holds true for C++.
In any case, a better approach to converting binary representations may be to use a reinterpret_cast:
int int_val = 2000;
float float_val = reinterpret_cast< float & >(int_val);

Unfortunately once again the C++ Final Draft Standard in not overly clear on how valid this is, but I believe it is "better" than the union trick. In any event, it better expresses intent and danger.

Enigma

EDIT: Typo fix.

[Edited by - Enigma on January 20, 2006 6:08:36 AM]

Share this post


Link to post
Share on other sites
Isn't the union concept dangerous anyway? Why would you want to store an int, then reference it as a double without doing type conversion? If you store using the double ref to the union, then try to read the int union value, you won't get the int equivalent.

Share this post


Link to post
Share on other sites
Quote:
Original post by spoulson
Isn't the union concept dangerous anyway?


You might want to peform an endian swap or something like that. That kind of thing is always going to be implementation defined.

Share this post


Link to post
Share on other sites
That code may work, but if it does work, it will construct it from a temporary, and changing the value of the reference (ref1) won't change the original value (val1).

Share this post


Link to post
Share on other sites
Quote:
Original post by spoulson
Why would you want to store an int, then reference it as a double without doing type conversion? If you store using the double ref to the union, then try to read the int union value, you won't get the int equivalent.


The point is, sometimes you want to coerce the type like that, for example to get a float's bit representation.

The idiomatic way of performing a type coercion in C++ is to do it by casting to a reference of the required type, preferably using a reinterpret_cast<>().

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
[...]Unfortunately once again the C++ Final Draft Standard in not overly clear on how valid this is, but I believe it is "better" than the union trick. In any event, it better expresses intent and danger.[...]
I'm fairly certain that just about every 'real' use of 'reinterpret_cast' is implementation-defined. IMO, it's basically included to allow implementation-defined things that would not otherwise be allowed (except possibly via a union, but that isn't as explicit as a reinterpret_cast)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!