2
votes

C++11 standard require that the template member of std::pair<> must have const copy constructor. Otherwise, it would not compile.(from the book the C++ standard library, Nicolai M. Josuttis.). So, code below wouldn't compile if it's against c++11 standard:

class A{
    int i;
public:
    A(){}
    A(A&){} 
};

int main(){
    pair<int, A> p;
}

with -std=c++11, the G++ compiler would report error:

constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = int; _T2 = A]' declared to take const reference, but implicit declaration would take non-const:

            constexpr pair(const pair&) = default

That is because we declare A's copy constructor to be non-const. If we change A(A&){} to be A(const A&){}, or we remove the -std=c++11 flag, everything would be fine.

My question is, how is that constaint implemented? I can see no support in the language itself that provide us a inside look into the template parameter T. I mean, with something declared like this:

template<typename T1, typename T2>
class pair{
    ...
    //how do you know whether the constructor of T1 and T2 are const ?
    ...
};

How do you know whether the constructor of T1 and T2 is are const or not

1

1 Answers

3
votes

//how do you know whether the constructor of T1 and T2 are const ?

You use std::is_copy_constructible.