Search code examples
c++destruction

Check destruction order constraints at compile time


Is there a way to check destruction order constraints of the form "A is destructed before B" at compile time?


Solution

  • I don't think that's possible. Take for instance the following code:

    int main(int argc, char **argv){
      Object A = new Object(), B = new Object();
      if(argc == 1){
        delete A;
        delete B;
      }else
      {
        delete B;
        delete A;
      }
    }
    

    How would you know at compile time which destructor is called first?