c++ making a union inside a class

Started by
24 comments, last by phresnel 15 years, 4 months ago
Quote:Original post by DevFred
Quote:Original post by phresnel
Of course sizeof(x) == sizeof(reference_to_x)

No, sizeof(x) == sizeof(vector[0]), because x is an alias for vector[0].


Sorry, but you've misunderstood my post. It was my obviously poor try on giving a generalised answer on what was a question implicated by a wrong statement earlier:
template <typename T> struct s {   T x;   T &reference_to_x;   enum { are_the_same_size = sizeof (x) == sizeof (reference_to_x) };}...std::cout << static_cast <int> (s<some_type>::are_the_same_size) << '\n';

... prints 1 for every some_type.
Advertisement
SiCrane whilst that is nice, I wonder how it solves the OP's quesiton
Quote:I wouldn't like to get rid of the array since I pass it to GL using the *v calls

They variables x,y and z are still subject to padding are they not? so how would you return a pointer to float using this code.
Quote:Original post by dmail
Quote:I wouldn't like to get rid of the array since I pass it to GL using the *v calls

They variables x,y and z are still subject to padding are they not? so how would you return a pointer to float using this code.
The default padding for consecutive 32-bit floats will be zero on pretty much any normal platform, so you should be fairly safe in just taking the address of x. This may not be 100% portable, but I have been doing it for some years, across Mac/Windows/Linux and ppc/x86/ppc64/i64, with no problems.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
Quote:Original post by dmail
Quote:I wouldn't like to get rid of the array since I pass it to GL using the *v calls

They variables x,y and z are still subject to padding are they not? so how would you return a pointer to float using this code.
The default padding for consecutive 32-bit floats will be zero on pretty much any normal platform, so you should be fairly safe in just taking the address of x. This may not be 100% portable, but I have been doing it for some years, across Mac/Windows/Linux and ppc/x86/ppc64/i64, with no problems.


Yes but the point of SiCrane's post was
Quote:... a trick that is legal standard C++...

I had already noted that you could take the address of x, yet just to be sure align the class to avoid any potential padding.
Maybe there is some way of using this trick but personally I do not see it. If somebody else asks then SiCrane may well tell.
Quote:Original post by dmail
I had already noted that you could take the address of x, yet just to be sure align the class to avoid any potential padding.
Maybe there is some way of using this trick but personally I do not see it. If somebody else asks then SiCrane may well tell.


This has been gone over many times in these forums. Just use a static_assert that guarantees that sizeof(my_vec3f_t) == sizeof(float * 3). Here are some links to past discussions:

a slick trick in C++
[C++, math] vector
--Michael Fawcett
Quote:Original post by mfawcett
Quote:Original post by dmail
I had already noted that you could take the address of x, yet just to be sure align the class to avoid any potential padding.
Maybe there is some way of using this trick but personally I do not see it. If somebody else asks then SiCrane may well tell.


This has been gone over many times in these forums. Just use a static_assert that guarantees that sizeof(my_vec3f_t) == sizeof(float * 3). Here are some links to past discussions:

a slick trick in C++
[C++, math] vector


Ok I give up. Use a compile time assert to check for a non standard feature. :)

edit:
LoL
One last thing. :)
If you go by the assumption that there is no padding added, what is the difference between the code posted above and the following

template<class T>class Vector3{public:   Vector3(const T &xx, const T &yy, const T &zz) :      x(xx), y(yy), z(zz)   {   };   T& operator[](size_t idx)   {      return *(&x + idx);   };   const T& operator[](size_t idx) const   {      return *(&x + idx);   };   T x,y,z;};int main(){   Vector3<float> vec(1,2,3);   vec[0] = 5;   std::cout << vec.x << std::endl;}


[Edited by - dmail on December 9, 2008 12:48:48 PM]
Only that you are forcing the Undefined Behavior on users of your class. The slick trick method allows you to implement your class with Standard C++.

If the user decides to invoke Undefined Behavior (but almost always portable as long as the static_assert holds), that's up to him, and he can do it by taking the address of x in either case.

Basically the difference is:

slick trick = no UB
what you just posted = UB
--Michael Fawcett
mfawcett you really do not get it do you.
The OP wants to be able to pass a float pointer to OpenGL, your answer is just to return the address of x. Yet this relies on compiler specific behaviour and is not available with the "slick trick". So "If you go by the assumption that there is no padding added" why use the slick trick in the first place.
Quote:Original post by dmail
mfawcett you really do not get it do you.

I see why your user rating is so low. ;)
I assure you, I really *DO* get it.

Quote:
The OP wants to be able to pass a float pointer to OpenGL, your answer is just to return the address of x. Yet this relies on compiler specific behaviour and is not available with the "slick trick". So "If you go by the assumption that there is no padding added" why use the slick trick in the first place.

Passing a float pointer to OpenGL is outside the scope of the class. Your last post asked what is different between the slick trick and the code you posted. I told you - One is defined, the second isn't. Passing a float pointer to OpenGL with either is UB.
--Michael Fawcett
Quote:I see why your user rating is so low. ;)

You have the wrong end of the stick here once again.
Quote:
Passing a float pointer to OpenGL is outside the scope of the class.

No it is not it is what the OP wants!
Quote:I wouldn't like to get rid of the array since I pass it to GL using the *v calls


Quote:Your last post asked what is different between the slick trick and the code you posted.
No this is not what I asked let me quote myself as you can not seem to read it for yourself.
Quote:If you go by the assumption that there is no padding added, what is the difference between the code posted above and the following


[edit]
Quote:You asked for the difference between the slick trick and your code. I told you once, and I'll tell you again yap yap yap...

It seems bold it not enough for you,
If you go by the assumption that there is no padding added what is the difference between the code posted above and the following

[Edited by - dmail on December 10, 2008 5:38:39 PM]

This topic is closed to new replies.

Advertisement