Questions tagged [pseudo-destructor]

A destructor for a non-class type, primarily used for calling destructors in templates

In C++, non-class types, such as int, do not have a destructor. However, the compiler will generate an empty (no-op) psuedo-destructor should such a call be necessary for the implementation of a template or in other cases where the type of a variable may not necessarily be known (e.g. external typedef).

For example, both calls to foo in the following program would be valid:

#include <vector>

template <typename T>
void foo()
{
    T x;
    // explicitly call destructor
    x.~T();
}

int main()
{
    foo<int>();
    foo< std::vector<int> >();
}

This is true despite the fact that no destructive action will be taken on the int.

5 questions
9
votes
2 answers

Valid syntax of calling pseudo-destructor for a floating constant

Consider the following demonstrative program. #include int main() { typedef float T; 0.f.T::~T(); } This program is compiled by Microsoft Visual Studio Community 2019. But clang and gcc issue an error like this prog.cc:7:5:…
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3
votes
1 answer

Pseudo-destructor call with template keyword

The following code does not compile with clang 5.0.0 (compilation flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors -O0): struct foo { }; int main() { foo f; f.~decltype(f)(); // OK f.template ~decltype(f)(); // OK …
3
votes
3 answers

Unqualified pseudo-destructor-name

This simple program is accepted by EDG (ICC) but rejected by GCC and Clang. Is it well formed? If not, why? int main() { int n; n.~int(); } To the curious: The program doesn't do anything and I rather doubt there's even a use case for this…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
1
vote
1 answer

Why does the standard disallow a pseudo-destructor call with a name of scalar type?

The standard rules: [expr.prim.id.unqual]/nt:unqualified-id: unqualified-id: ... ~ type-name ~ decltype-specifier ... [dcl.type.simple]/nt:type-name: type-name: class-name enum-name typedef-name A name of scalar type isn't a type-name, so…
0
votes
1 answer

Explicitly defined destructor for scalar type

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…
user2953119