Is it possible to use 'enable_if' and 'is_same' with variadic function templates?

后端 未结 3 385
一向
一向 2020-12-10 06:09

These two non-variadic function templates do compile:

template 
typename std::enable_if::value, v         


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 06:45

    Yes. You can use a fold expression in C++17:

    template 
    typename std::enable_if<(std::is_same::value && ...), void>::
    type testFunction(T a, U... bs) {
        std::cout << "bs are floats\n";
    }
    

    In C++11, you can reimplement std::conjunction:

    template struct conjunction : std::true_type { };
    template struct conjunction : B1 { };
    template
    struct conjunction 
        : std::conditional_t, B1> {};
    

    template 
    typename std::enable_if<
        std::conjunction_v...>, 
        void
    >::type testFunction(T a, U... bs) {
        std::cout << "bs are floats\n";
    }
    

提交回复
热议问题