 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ulrich Eckhardt Guest
|
Posted: Fri Jan 23, 2004 9:15 pm Post subject: (portability of) memberfunctions in unions |
|
|
Hello!
In STLport is a union to create a (properly aligned) buffer. It is used for
global objects that are explicitly created on that storage(with placement
new).
The union looks like this (src/aligned_buffer.h):
template<class T>
union aligned_buffer {
char buf[sizeof(T)];
struct { double a; double b; } padding;
};
The usual modus operandi was
aligned_buffer<ostream> cout_;
...
function_taking_ostream_ptr( reinterpret_cast<ostream*>(&cout_) );
I then added
T* operator&()
{ return reinterpret_cast<T*>(this); }
T const* operator&() const
{ return reinterpret_cast<T const*>(this); }
to the union, hoping to improve type-security and reduce code. It then
allowed things like
function_taking_ostream_ptr( &cout_ );
My questions are
- Is this syntax valid C++?
- Are there any (real-world) compilers known that reject such code?
- Is there a better way for this?
- Can anyone guess why the padding in the union has two doubles?
thank you
Ulrich Eckhardt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Tue Jan 27, 2004 2:55 pm Post subject: Re: (portability of) memberfunctions in unions |
|
|
"Ulrich Eckhardt" <doomster (AT) knuut (DOT) de> wrote
Hello,
| Quote: | template<class T
union aligned_buffer {
char buf[sizeof(T)];
struct { double a; double b; } padding;
};
....
I then added
T* operator&()
{ return reinterpret_cast
T const* operator&() const
{ return reinterpret_cast<T const*>(this); }
to the union, hoping to improve type-security and reduce code. It then
allowed things like
function_taking_ostream_ptr( &cout_ );
My questions are
- Is this syntax valid C++?
Yes. Member functions are allowed in unions as much as in structs |
and classes. The standard only specifies few restrictions for
unions (no base class allowed, restrictions on data members).
Only *anonymous* unions are specifically not allowed to have
member functions. (9.5/2: "An anonymous union shall not have function
members.")
| Quote: | - Are there any (real-world) compilers known that reject such code?
Not the one I use. |
| Quote: | - Is there a better way for this?
It is usually considered error-prone to overload operator& |
(it could have weird side-effects in generic code).
I would rather use a named member function (e.g. get() ).
| Quote: | - Can anyone guess why the padding in the union has two doubles?
Not me. I do not believe it is supposed to change alignment |
requirements for the struct. Maybe it did on some platform?
hth, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|