On 09/05/2012 21:15, Asger Joergensen wrote:
> Hi PiotrN
>
> PiotrN wrote:
>
>> Hello,
>>
>> Two questions:
>> 1) Why I cannot call destructor for builtin types like int:
>> int a;
>> int* p = new int[7];
>> p->~int();
>
> You never call the destructor directly you use delete instead:
Untrue. You use a destructor call whenever you want to retain the
underlying memory
>
> int* p = new int[7];
>
> ....do some work
>
> delete[] p;
All that is fine until you are using a placement new in a context where
calling delete is wrong. For example, when using a memory pool.
The real problem with the above is that if you are using a dtor call you
cannot apply it to p because p is a pointer to an array.
However there is also the problem that you cannot use the dtor syntax on
a buitin type by its formal name only when using an alias either
provided through a typedef or by a template parameter.
I think the argument is that if you know the name you know it is trivial
so nothing needs to be done. That you can use the syntax on an alias
provided through a typedef is an artefact of way we provided template
type names. It is also not irrational because if you want to change some
code by replacing a usage of int by a usage of some other type you would
want it to continue to work. E.g.
I write all by code using:
typedef int year;
and at a later stage decide I want to provide a real year type (called
my_year_type which has a non-trivial dtor then I can simply replace the
typedef with:
typedef my_year_type year;
and the code should continue to work.
Another way of looking at this is that an int is an int and there is no
way you can make it anything else but and INT may stop being an int and
become anything else the programmer wants it to be which may not be a
type with a trivial dtor.