在以下代碼中,可變參數構造函數被調用兩次。如何在適當的情況下調用拷貝構造函數而不是可變參數構造函數的單個參數版本?如何獲取通過可變參數構造函數調用的複製構造函數?
#include <iostream>
struct Foo
{
Foo(const Foo &)
{
std::cout << "copy constructor\n";
}
template<typename... Args>
Foo(Args&&... args)
{
std::cout << "variadic constructor\n";
}
std::string message;
};
int main()
{
Foo f1;
Foo f2(f1); // this calls the variadic constructor, but I want the copy constructor.
}
還有一點點,就是參考摺疊(怎麼叫?),即'T &&&'=>'T&'因爲否則左值('f1')不能綁定到'T &&'。 –