Why must a base class destructor be accessible only when a custom constructor is declared?

后端 未结 2 1328
终归单人心
终归单人心 2021-02-04 04:38

Comeau, g++ (ideone) and EDG accept the following code without diagnostic. Visual C++ compiles successfully, albeit with warning C4624.

class indestructible_bas         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 04:47

    Well, if the automatically-generated constructor calls a possibly-throwing constructor, then it will give the same access error.

    #include 
    
    class indestructible_base
    {
      ~indestructible_base();
      std::string s; // <------ this may throw
    };
    
    class T : indestructible_base
    {
    public:
      //T() {}
    };
    
    int main(void) { new T(); }
    

    So I guess exceptions is the answer. In ANSI ISO IEC 14882 the only noexcept(true) string constructor is the move constructor. I believe this should compile but ideone says no.

提交回复
热议问题