相关疑难解决方法(0)

如何获得通过可变参数构造函数调用的复制构造函数?

在以下代码中,可变参数构造函数被调用两次.在适当的时候,如何才能调用复制构造函数而不是可变参数构造函数的单个参数?

#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.
}
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

11
推荐指数
2
解决办法
1008
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1

variadic-templates ×1