Is this standard

  • Thread starter stewart.tootill
  • Start date
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
 
M

msalters

(e-mail address removed) schreef:
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.

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.

It's legal. There are a number of variantions that are allowed, and
naming a dtor is one area in which older compilers might fail. Don't
woory too much, though, this particular variant is one of the easier
variations.

Regards,
Michiel Salters
 
S

stewart.tootill

Thanks for that. I don't have to be cross compiler compliant but its
nice to know i am being.
 
U

Uenal Mutlu

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)

It is ok.
Another alternative is the following, but it's slower:

my_obj::~my_obj()
{
for ( size_t pos = 0; pos < noofElements; ++pos )
{
my_nested_type item = my_array[pos]
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top