考慮下面的代碼爲什麼調用轉發引用構造函數而不是複製構造函數?
#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