Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ destructor return type

A destructor is a special member function that takes no arguments and has no return type: this is told in pretty much all the c++ books. However, in the libstd++ library, it uses the following to test if a type is destructible,

  struct __do_is_destructible_impl
  {
    template<typename _Tp, typename _U = decltype(declval<_Tp&>().~_Tp())>
      static true_type __test(int);

    template<typename>
      static false_type __test(...);
  };

Gnu g++ would show _U with typeid void, so, destructor does return a type? Experts please explain what c++ standard says about this.

like image 724
Bill Avatar asked Mar 18 '19 04:03

Bill


People also ask

Is return 0 a destructor?

When exit(0) is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used. Calling destructors is sometimes important, for example, if destructor has code to release resources like closing files,deleting dynamically allocated memory etc.

What value should return a destructor?

Destructors do not return a value.

Can you return from destructor C++?

Destructor function is automatically invoked when the objects are destroyed. It cannot be declared static or const. The destructor does not have arguments. It has no return type not even void.

Does constructor and destructor have return type?

Constructors and destructors do not have return types nor can they return values. References and pointers cannot be used on constructors and destructors because their addresses cannot be taken.


1 Answers

Note that the code you are considering is checking the return type of an explicit destructor call expression. This does not give any meaning to the return type of the destructor itself.

An explicit destructor call expression may or may not be a function call expression (it is if the type is a class with a destructor, it isn't if the type has trivial destruction because it is a non-class type). Function calls are described in [expr.call] which has the following special rule for explicit destructor calls:

If the postfix-expression designates a destructor, the type of the function call expression is void; otherwise, the type of the function call expression is the return type of the statically chosen function (i.e., ignoring the virtual keyword), even if the type of the function actually called is different. This return type shall be an object type, a reference type or cv void.

Where this does not apply are pseudo destructor calls, described in [expr.pseudo] (Pseudo destructor call), which states the following:

The use of a pseudo-destructor-name after a dot . or arrow -> operator represents the destructor for the non-class type denoted by type-name or decltype-specifier. The result shall only be used as the operand for the function call operator (), and the result of such a call has type void. The only effect is the evaluation of the postfix-expression before the dot or arrow.

As you can see, in neither case is the clause reached that makes the function return type and function call expression type the same. Thus the expression has type void even though the (special member) function has no return type at all.

like image 108
Ben Voigt Avatar answered Oct 02 '22 02:10

Ben Voigt