在下面的代码中,可变参数构造函数被调用两次。在合适的情况下,如何获取拷贝构造函数而不是可变参数构造函数的单参数版本?

#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.
}

最佳答案

实际上,这与构造函数是可变参数这一事实无关。以下具有非可变构造函数模板的类表现出相同的行为:

struct Foo
{
    Foo() { }

    Foo(const Foo& x)
    {
        std::cout << "copy constructor\n";
    }

    template <typename T>
    Foo(T&& x)
    {
        std::cout << "template constructor\n";
    }

};

问题在于构造函数模板是更好的匹配。要调用复制构造函数,需要进行资格转换以将非const左值f1绑定(bind)到const Foo&(必须添加const限定条件)。

要调用构造函数模板,不需要进行任何转换:T可以推导为Foo&,在引用折叠后(Foo& &&-> Foo&),参数x的类型为Foo&

您可以通过提供另一个具有非常量左值引用参数Foo&的拷贝构造函数来解决此问题。

09-26 22:23
查看更多