Connecting Tech Pros Worldwide Help | Site Map

Is this standard

stewart.tootill@softel.co.uk
Guest
 
Posts: n/a
#1: Jul 23 '05
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

msalters
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Is this standard



stewart.tootill@softel.co.uk schreef:[color=blue]
> Hello,
>
> I have a template class, which holds a fixed size array of elements[/color]
in[color=blue]
> it. It can't be a vector because the elements aren't copy[/color]
constructable[color=blue]
> and the whole thing is an optimisation to avoid heap allocation[/color]
anyway,[color=blue]
> 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[/color]
to[color=blue]
> 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.[/color]

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

stewart.tootill@softel.co.uk
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Is this standard


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

Uenal Mutlu
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Is this standard


<stewart.tootill@softel.co.uk>[color=blue]
>
> 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)[/color]

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]
}
}


Closed Thread