Re: size of Empty Class
On Mar 13, 4:07 pm, "Alf P. Steinbach" <al...@start.no> wrote:
* Marco Nef:
What does standards says about it ?
It allows that size to be anything >0.
However, the sizeof(T) does not necessarily reflect the size used in
memory in the context of inheritance.
In that context the compiler is free to optimize away an empty base
class sub-object, called the Empty Base Class optimization.
I can reproduce that an empty class has 1 byte in VisualC++, what I'm
quite surprised about. But on the other hand Bjarne Stroustrup writes i=
n
his book "The C++ programming language" that "the sizeof operator yield=
s
the size, in bytes, of its operand". There is nothing like ">0". Where
have you found that information?
The C++ standard, on sizeof.
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url:http://alfps.izfree.com=
/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linkin=
g
to it is even better! Thanks in advance!
namespace nirved{
struct void_{};
template<typename T = void_> struct ordinal;
template<> struct ordinal<void_>
{
typedef void_ previous_type;
};
template<> struct ordinal<ordinal<void_> >
: private ordinal<void_>
{
typedef ordinal<void_> previous_type;
previous_type x;
void_ x;
};
template<> struct ordinal<ordinal<ordinal<void_> > >
: private ordinal<ordinal<void_> >
{
typedef ordinal<ordinal<void_> > previous_type;
previous_type x;
void_ x;
};
template<typename T> struct ordinal//:
public T
{
typedef T previous_type;
previous_type x;
void_ x;
};
}
int main()
{
typedef nirved::ordinal<> zero;
typedef nirved::ordinal<zero> one;
typedef nirved::ordinal<one> two;
typedef nirved::ordinal<two> three;
typedef nirved::ordinal<three> four;
typedef nirved::ordinal<four> five;
std::cout << sizeof(five);
return 0;
}
Thus sizeof(five)==5 has undefined result ?
With Regards,
Reetesh Mukul