【发布时间】:2017-09-04 14:06:29
【问题描述】:
给定以下代码
#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&&)
为什么复制构造函数被解析为模板化转发引用构造函数而不是移动构造函数?我猜这是因为移动构造函数是隐式定义的,而不是复制构造函数。但是在阅读了在堆栈溢出中未隐式定义复制构造函数的情况后,我仍然感到困惑。
【问题讨论】:
-
这是一个相当常见的博客主题:akrzemi1.wordpress.com/2013/10/10/too-perfect-forwarding
标签: c++ c++11 constructor c++14 implicit-constructor