c++ - Destructors of builtin types (int, char etc..) -


in c++ following code gives compiler error:

void destruct1 (int * item) {   item->~int(); } 

this code same, typedef int type , magic happens:

typedef int myint;  void destruct2 (myint * item) {   item->~myint(); } 

why second code works? int gets destructor because has been typedefed?

in case wonder why 1 ever this: comes refactoring c++ code. we're removing standard heap , replacing selfmade pools. requires call placement-new , destructors. know calling destructors primitive types useless, want them in code nevertheless in case later replace pods real classes.

finding out naked int's don't work typedefed ones quite surprise.

btw - have solution involves template-functions. typedef inside template , fine.

it's reason makes code work generic parameters. consider container c:

template<typename t> struct c {     // ...     ~c() {         for(size_t = 0; i<elements; i++)             buffer[i].~t();     } }; 

it annoying introduce special cases built-in types. c++ allows above, if t happens equal int. holy standard says in 12.4 p15:

the notation explicit call of destructor can used scalar type name. allowing makes possible write code without having know if destructor exists given type.

the difference between using plain int , typedef'ed int syntactically different things. rule is, in destructor call, thing after ~ type-name. int not such thing, typedef-name is. in 7.1.5.2.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -