C 11标准要求 std::pair<>
的模板成员必须具有 const 复制构造函数 . 否则,它就不会编译 . (从书中可以看出C标准库,Nicolai M. Josuttis . ) . 所以,下面的代码对于c 11标准不会是't compile if it':
class A{
int i;
public:
A(){}
A(A&){}
};
int main(){
pair<int, A> p;
}
使用 -std=c++11
,G编译器将报告错误:
constexpr std :: pair <_T1,_T2> :: pair(const std :: pair <_T1,_T2>&)[with _T1 = int; _T2 = A]'声明采用const引用,但隐式声明将采用非const:constexpr对(const pair&)= default
那是因为我们将A的复制构造函数声明为非const . 如果我们将 A(A&){}
更改为 A(const A&){}
,或者我们删除 -std=c++11
标志,一切都会好的 .
我的问题是,这个constaint是如何实现的?我看不到语言本身的支持,它们为我们提供了模板参数 T
的内部视图 . 我的意思是,宣布这样的事情:
template<typename T1, typename T2>
class pair{
...
//how do you know whether the constructor of T1 and T2 are const ?
...
};
你怎么知道T1和T2的构造函数是否是 const
1 回答
你使用std::is_copy_constructible .