Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;-推导参数包(可变模板)构造函数并通过启用\u if\u t复制构造函数_C++_Templates_C++14_Sfinae_Variadic - Fatal编程技术网

C++ C++;-推导参数包(可变模板)构造函数并通过启用\u if\u t复制构造函数

C++ C++;-推导参数包(可变模板)构造函数并通过启用\u if\u t复制构造函数,c++,templates,c++14,sfinae,variadic,C++,Templates,C++14,Sfinae,Variadic,更新: 谢谢你,大露营。 这是最后的结构A struct A { template<class ... Args,class=std::enable_if_t<(sizeof...(Args)!=1)>> A(Args &&...args) { cout<<'v'; } template<class Arg,class=std::enable_if_t<!std::is_bas

更新:
谢谢你,大露营。
这是最后的
结构A

struct A
{
    template<class ... Args,class=std::enable_if_t<(sizeof...(Args)!=1)>>
    A(Args &&...args)
    {
        cout<<'v';
    }
    template<class Arg,class=std::enable_if_t<!std::is_base_of<std::remove_reference_t<Arg>,A>::value>>
    A(Arg &&arg)
    {
        cout<<'v';
    }
    A(const A &)
    {
        cout<<'c';
    }
    A(A &&)
    {
        cout<<'m';
    }
};
结构A { 模板 A(Args&&…Args) { 库特 A(Arg&&Arg) {
cout因为当您将
b
c
传递给
c
d
的构造函数时,
Args和
c
A&
的编译器(不符合常量条件),因此
Args和…Args
常量A&
更匹配

要实现您的目标,您可以通过以下方式实现:

struct A
{
    A() = default;

    template<class ... Args,std::enable_if_t<(sizeof...(Args)>1), bool> = true>
    A(Args &&...args)
    {
        cout<<'v';
    }

    template<class Arg,std::enable_if_t<!std::is_base_of<A, std::remove_reference_t<Arg>>::value, bool> = true>
    A(Arg && arg)
    {
        cout<<'v';
    }

    A(const A &)
    {
        cout<<'c';
    }
    A(A &&)
    {
        cout<<'m';
    }
};
结构A { A()=默认值; 模板=真> A(Args&&…Args) { 库特 A(Arg&&Arg) {
如果使用未展开的
Args
,这应该是格式错误的。但是如果使用
std::remove\u reference\t
,则会发生编译错误。在本例中,始终需要默认构造函数。如果未显式指定,请确保改为写入
sizeof…(Args)!=1
struct A
{
    A() = default;

    template<class ... Args,std::enable_if_t<(sizeof...(Args)>1), bool> = true>
    A(Args &&...args)
    {
        cout<<'v';
    }

    template<class Arg,std::enable_if_t<!std::is_base_of<A, std::remove_reference_t<Arg>>::value, bool> = true>
    A(Arg && arg)
    {
        cout<<'v';
    }

    A(const A &)
    {
        cout<<'c';
    }
    A(A &&)
    {
        cout<<'m';
    }
};