考虑下面的代码为什么调用转发引用构造函数而不是复制构造函数?
#include <iostream>
using namespace std;
template <typename Type>
struct Something {
Something() {
cout << "Something()" << endl;
}
template <typename SomethingType>
Something(SomethingType&&) {
cout << "Something(SomethingType&&)" << endl;
}
};
int main() {
Something<int> something_else{Something<int>{}};
auto something = Something<int>{};
Something<int>{something};
return 0;
}
我得到以下输出
Something()
Something()
Something(SomethingType&&)
为什么拷贝构造函数解析成模板转发参考构造函数,但不是移动构造函数?我猜测这是因为移动构造函数是隐式定义的,而不是复制构造函数。但是在阅读堆栈溢出中未隐式定义拷贝构造函数的情况后,我仍然感到困惑。
这是一个体面的共同话题博客:https://akrzemi1.wordpress.com/2013/10/10/too-perfect-forwarding/ – chris