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

后端 未结 3 376
一向
一向 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:36

    Or you could simply make use of additional variadic pack to test if all template arguments are the same and equal to a given type (C++11):

    #include 
    #include 
    
    template 
    struct pack { };
    
    template 
    typename std::enable_if, pack>::value, void>::
    type testFunction(T a, U... bs) {
        std::cout << "bs are integers\n";
    }
    
    template 
    typename std::enable_if, pack>::value, void>::
    type testFunction(T a, U... bs) {
        std::cout << "bs are floats\n";
    }
    
    int main() {
        testFunction(1, 2, 3, 4, 5);
        testFunction(1, 2.0f, 3.5f, 4.4f, 5.3f);
    }
    

    [live demo]

    Output:

    bs are integers
    bs are floats
    

提交回复
热议问题