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

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

    std::map uses std::pair to store key-value pairs, where the key (the first element) is const.

    The compiler error relates to the required copy constructor for std::pair, even if it isn't being used (which I don't think it is).

    std::pair has to be generated. This is first required with the call to map::begin. Since no explicit copy constructor is given for this type, the implicit one used.

    The implicit constructor will have signature T::T(const T&) only if all non-static members of T, (type S), have copy constructors S::S(const S&) (the same requirement has to hold for T's base types copy constructors). Otherwise a copy constructor with signature T::T(T&) is used instead.

    A's copy constructor fails this requirement, so std::pair::pair has the wrong signature for the STL, which requires T::T(const T&).

提交回复
热议问题