Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/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++ 可变模板类-0长度包构造函数冲突_C++_Variadic Templates_Sfinae - Fatal编程技术网

C++ 可变模板类-0长度包构造函数冲突

C++ 可变模板类-0长度包构造函数冲突,c++,variadic-templates,sfinae,C++,Variadic Templates,Sfinae,给定 但我不知道如何/为什么或者它是否可靠 我也可能会使用类似 template<Args...> foo(const std::function<void(Args...)>& func) : m_function{ func } { cout << "ctor 2" << endl; } std::启用 如何使用std::enable_if解决这个问题,以及我的第二个代码示例中发生了什么?struct foo{ s

给定

但我不知道如何/为什么或者它是否可靠

我也可能会使用类似

template<Args...>
foo(const std::function<void(Args...)>& func) :
    m_function{ func } {

    cout << "ctor 2" << endl;
}
std::启用
如何使用
std::enable_if
解决这个问题,以及我的第二个代码示例中发生了什么?

struct foo{
std::enable_if<sizeof...(Args) != 0, ???>
使用Func=std::function; foo(常量Func&Func,Args…Args){…} 结构无{}; 使用A=typename std::conditional 0,Func,none>::type; foo(const A&func){…};
正如Johannes Schaub-litb在评论中指出的,您可以简单地添加一个未使用模板参数的可变列表,只需将模板中的第二个构造函数转换为模板构造函数,并将优先级(避免冲突)赋予第一个非模板构造函数

所以你可以简单地写

struct foo {
    using Func = std::function<void(Args...)>;
    foo(const Func& func, Args... args)  { ... }

    struct none {};
    using A = typename std::conditional<sizeof...(Args) > 0, Func, none>::type;

    foo(const A& func) { ... };

但这总是会导致两个构造函数,其中一个可能会尝试构造一个
foo f(foo::none())
?@Aconcagua如果您担心的话,您可以将
none
设为私有。这完全足够了:
模板foo(const std::function&func)
。DummyArgs将始终默认为sizeof 0,模板将始终存在。在零的情况下,编译器将首选非模板构造函数。@Johanneschaub litb模板参数可以不命名以将其进一步缩短。或者,如果sizeof Args…大于ze,则只需要调用第二个ctor因此,您可以这样编写构造函数:
foo(std::false\u类型,const std::function&func,Args…Args);foo(std::true\u类型,const std::function&func);
,并放置一个代理构造函数,如
模板foo(const std::function&func,Args1…Args):foo(std::bool\u常量0&&sizeof…(Args1)==0>(),func,args…{}
。通过“我希望只有当sizeof…(args)!=0时才会实例化ctor2”,您的意思是说“我希望只有当sizeof…(args)==0时才会实例化ctor2”?除了std::function right之外,ctor2不需要任何参数?@PinkTurtle如果您使用
unique\u ptr
的原因是提供一个无参数的给定状态,并且您手头有c++17,那么您可能希望改为使用
std::optional
。是什么使得
bool B=(sizeof…(args)>0u),std::enable_if_t=true
工作,而
std::enable_if_t 0u),bool>=true
失败?@Timo-SFINAE,通过构造函数和其他方法,在基于涉及构造函数/方法本身的模板参数的测试时工作;因此
std::enable_if_t 0u),bool>=true
失败,因为
sizeof…(Args)>0u
是基于类的模板参数而不是构造函数的测试。使用
bool B=(sizeof…(Args)>0u
可以创建构造函数的模板参数
B
,该构造函数参与以下
std::enable_if
测试。
struct foo {
    using Func = std::function<void(Args...)>;
    foo(const Func& func, Args... args)  { ... }

    struct none {};
    using A = typename std::conditional<sizeof...(Args) > 0, Func, none>::type;

    foo(const A& func) { ... };
template <typename ...>
foo (std::function<void(Args...)> const & func)
    : m_function{ func }
 { std::cout << "ctor 2" << std::endl; }
template <bool B = (sizeof...(Args) > 0u),
          std::enable_if_t<B, bool> = true>
foo (std::function<void(Args...)> const & func)
    : m_function{ func }
 { std::cout << "ctor 2" << std::endl; }