S
stewart.tootill
Hello,
I have a template class, which holds a fixed size array of elements in
it. It can't be a vector because the elements aren't copy constructable
and the whole thing is an optimisation to avoid heap allocation anyway,
so vector doesn't fit the bill.
Anyway, the elements inside it are in a suitably aligned chunk of
memory, which I am constructing using placement new. So far so good.
Unfortunately the typename is quite long winded, so I have a typedef to
it. For the sake of argument, we'll call it my_nested_type.
So the constructor looks like this
my_obj::my_obj( int a, int b )
{
for ( size_t pos = 0; pos < noofElements; ++pos )
new (&my_array[pos]) my_nested_type( a, b );
}
and i'm pretty happy with that. When I wrote the destructor, I wrote
this
my_obj::~my_obj()
{
for ( size_t pos = 0; pos < noofElements; ++pos )
my_array[pos].~my_nested_type();
}
and I was surprised when it compiled. I know this is valid syntax for
calling a destructor of a class, but because it is a typedef I really
didn't expect it to compile.
Does anyone know if this is allowed in the standard, or whether MSVC
7.1 is just being helpful (I don't have any other compilers handy to
try it on)
Yours,
Stewart Tootill
I have a template class, which holds a fixed size array of elements in
it. It can't be a vector because the elements aren't copy constructable
and the whole thing is an optimisation to avoid heap allocation anyway,
so vector doesn't fit the bill.
Anyway, the elements inside it are in a suitably aligned chunk of
memory, which I am constructing using placement new. So far so good.
Unfortunately the typename is quite long winded, so I have a typedef to
it. For the sake of argument, we'll call it my_nested_type.
So the constructor looks like this
my_obj::my_obj( int a, int b )
{
for ( size_t pos = 0; pos < noofElements; ++pos )
new (&my_array[pos]) my_nested_type( a, b );
}
and i'm pretty happy with that. When I wrote the destructor, I wrote
this
my_obj::~my_obj()
{
for ( size_t pos = 0; pos < noofElements; ++pos )
my_array[pos].~my_nested_type();
}
and I was surprised when it compiled. I know this is valid syntax for
calling a destructor of a class, but because it is a typedef I really
didn't expect it to compile.
Does anyone know if this is allowed in the standard, or whether MSVC
7.1 is just being helpful (I don't have any other compilers handy to
try it on)
Yours,
Stewart Tootill