Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is the destructor of a constinit object called?

Generally it is said that the destructors of static objects are called in the reverse order of the constructors. As I understand, constinit objects are initialized at compile time, so their destructors should be called after the destructors of "normal" static objects.

The program

struct A
{
  constexpr A(const char* t): t_(t) {}
  ~A() {std::cout << "~A(" << t_ << ")\n";} 
  const char* t_;
};  

static A a1("static");  

int main () {
   static constinit A a2("constinit");  
   return 0;
} 

(using GCC 10), however, gives the output

~A(constinit)
~A(static)

i.e. the constinit object is destroyed before the "normal" static object (although it was constructed earlier). Is the "reverse order" rule no longer valid for constinit objects?

like image 545
Helmut Zeisel Avatar asked Jul 16 '20 05:07

Helmut Zeisel


People also ask

What are destructors when are they called?

A destructor is a member of a function which is automatically called when the class is destroyed. It has the same name as the class name but is preceded by a tilde (~). Normally a destructor is used to clean-up when the class is destroyed.

Is the destructor always called?

yes, when you delete something, the destructor is called.

Is the destructor called at the end of Main?

The destructor is called once the main function returns, i.e. after the return statement, just like local objects in any other function.

Is destructor called after return?

While returning from a function, destructor is the last method to be executed. The destructor for the object “ob” is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied & hence the output is i = 3.

When is a destructor called?

When is destructor called? A destructor function is called automatically when the object goes out of scope: (1) the function ends. (2) the program ends. (3) a block containing local variables ends. (4) a delete operator is called. How destructors are different from a normal member function? Destructors have same name as ...

What is the difference between a destructor and a constructor?

Remember that Constructor of an object is called immediately after the memory is allocated for that object and whereas the destructor is called just before deallocating the memory of that object. Yes, a destructor (a.k.a. dtor) is called when an object goes out of scope if it is on the stack or when you call delete on a pointer to an object.

What are the arguments of a destructor in C++?

The destructor does not have arguments. It has no return type not even void. An object of a class with a Destructor cannot become a member of the union. A destructor should be declared in the public section of the class.

What is the difference between an instance variable and a destructor?

An instance variable or an object is eligible for destruction when it is no longer reachable. A Destructor is unique to its class i.e. there cannot be more than one destructor in a class.


Video Answer


1 Answers

Both a1 and a2 are constant-initialized. The constinit specifier only asserts that the variable being defined is constant-initialized. So here a1 is initialized before a2 so a2 is destroyed before a1 as expected.

Is the "reverse order" rule no longer valid for constinit objects? The reverse order rule does not between constant initialized object and dynamicaly initialized object: even if construction of constant initialized object happens before construction of dynamicaly initialized object, destruction of constant initialized object is sequenced as if they had been dynamicaly initialized: [basic.start.term]/3

If an object is initialized statically, the object is destroyed in the same order as if the object was dynamically initialized.

like image 183
Oliv Avatar answered Oct 23 '22 03:10

Oliv