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

前端 未结 3 967
闹比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:35

    The copy constructor of std::pair isn't needed in this case, but because it is default defined inline in the declaration of std::pair, it is automatically instantiated along with the instantiation of std::pair itself.

    It would be possible for the standard library to provide a non-inline default definition of the copy constructor:

    template
      struct pair
      {
    // ...
        constexpr pair(const pair&);
    // ...
      };
    // ...
    template
    constexpr pair<_T1, _T2>::pair(const pair&) = default;
    

    However this would not accord with the strict letter of the standard (clause 20.3.2), where the copy constructor is default defined inline:

      constexpr pair(const pair&) = default;
    

提交回复
热议问题