Comparing two map::iterators: why does it need the copy constructor of std::pair?

前端 未结 3 1012
闹比i
闹比i 2021-02-04 03:35

The very simple code below compiles and links without a warning in C++98 but gives an incomprehensible compile error in C++11 mode.

#include 

struct          


        
3条回答
  •  半阙折子戏
    2021-02-04 04:37

    I think I found it after trying to reduce the error. First, the comparison doesn't seem required to make the program ill-formed. Then, the error message contained the dtor, so I tried not to instantiate the dtor. Result:

    #include 
    
    struct A {
        A(A& ); // <-- const missing
    };
    
    int main() {
        std::map* m = new std::map();
        // note: dtor not (necessarily?) instantiated
    }
    

    But the output message still contains, now for the line where the ctor of m is called:

    error: the parameter for this explicitly-defaulted copy constructor is const, but a member or base requires it to be non-const

     constexpr pair(const pair&) = default;
    

    Which hints to [dcl.fct.def.default]/4

    A user-provided explicitly-defaulted function (i.e., explicitly defaulted after its first declaration) is defined at the point where it is explicitly defaulted; if such a function is implicitly defined as deleted, the program is ill-formed.

    [emphasis mine]

    If, as I assume, [class.copy]/11 says that this ctor should be defined as deleted, then it is defined as deleted immediately - not only when it's odr-used. Therefore, an instantiation shouldn't be required to make the program ill-formed.

提交回复
热议问题