Reputation:
If we write the following code it works fine.
typedef int I;
I i;
int main()
{
i.~I();
}
I know that destructor is special member function (there is a definition from the Standard). But is there a way to explicitly define a function will call when pseudo-destructor is called? It is not clear why is it permitted to pseudo-destructor call for such types.
Upvotes: 0
Views: 166
Reputation: 119164
No, the standard says that a pseudo destructor call is a no-op. (More precisely, the only effect is the evaluation of the expression before the .
or ->
.)
The reason why pseudo destructor calls are allowed is so you can write template code that may call destructors without going to the trouble to explicitly check whether the type is a class.
Addendum: However, you can define a custom allocator whose destroy
function does something additional to merely destroying the object, so you can make it not be a no-op for int
. Note that a standard library container that takes an allocator argument will always use that allocator to destroy objects, instead of calling ~T()
on them directly.
Upvotes: 5