如何声明复制构造函数,如果我们有通用引用参数的构造函数,也?C++ 11构造函数与可变参数通用引用和复制构造函数
http://coliru.stacked-crooked.com/a/4e0355d60297db57
struct Record{
template<class ...Refs>
explicit Record(Refs&&... refs){
cout << "param ctr" << endl;
}
Record(const Record& other){ // never called
cout << "copy ctr" << endl;
}
Record(Record&& other){ // never called
cout << "move ctr" << endl;
}
};
int main() {
Record rec("Hello");
Record rec2(rec); // do "param ctr"
return 0;
}
根据这个构造器的表std::tuple
http://en.cppreference.com/w/cpp/utility/tuple/tuple [看看情况3,8]这个问题在标准库某种程度上解决了......但我无法通过STL的代码获得。
P.S.有些问题与C++ universal reference in constructor and return value optimization (rvo)有关
P.P.S.就目前而言,我只是增加了第一个参数Record(call_constructor, Refs&&... refs)
以用于真正的EXPLICIT调用。我可以手动检测,如果我们只有一个参数,如果它是Record
,并重定向呼叫复制ctr /参数ctr,但....我不能相信有没有这种标准的方式...
我不能写一个完整的答案,但这应该可以帮助你https://akrzemi1.wordpress.com/2013/10/10/too-perfect-forwarding/基本的想法是使用SFINAE。 –
@DanielJour这有点类似于我的P.附:部分,但thx,我很高兴知道这是已知的问题... – tower120
为了将来证明这个问题,应该指出*通用引用*将正式称为*转发引用*。该提案可以在[这里]找到(http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4164.pdf)。 – huu