Connecting Tech Pros Worldwide Help | Site Map

Is this standard

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 03:57 AM
stewart.tootill@softel.co.uk
Guest
 
Posts: n/a
Default Is this standard

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


  #2  
Old July 23rd, 2005, 03:57 AM
msalters
Guest
 
Posts: n/a
Default 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

  #3  
Old July 23rd, 2005, 03:57 AM
stewart.tootill@softel.co.uk
Guest
 
Posts: n/a
Default Re: Is this standard

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

  #4  
Old July 23rd, 2005, 03:58 AM
Uenal Mutlu
Guest
 
Posts: n/a
Default 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]
}
}


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.