Why is this template function compiled successfully?

Possible duplicate:
Built-in type destructors (int, char, etc.)

Template Function:

template<typename T> void kill(T* type) { type->~T(); } 

Call:

 int x= 5; kill(&x); 

woah, did it compile !? How does a primitive int type have a destructor? It also works with char , bool , etc.

+4
source share
2 answers

Section 12.4.16 of the Standard states

16 [Note: the designation for explicitly calling the destructor can be used for any scalar type name (5.2.4). This allows you to write code without knowing whether a destructor exists for this type. For instance,

 typedef int I; I* p; p->I::~I(); 

-end note]

+4
source

The relevant part of the standard is ยง5.2.4 / 1:

Using a pseudo-destructor-name after the dot. or an arrow โ†’ represents a destructor for a non-class type with the type name. The result should be used only as an operand for the function call operator (), and the result of such a call is of type void. The only effect is the evaluation of the postfix expression in front of the point or arrow.

+1
source

Source: https://habr.com/ru/post/1392611/


All Articles