C++ Variadic Function Templates of Known Type

前端 未结 4 1790
死守一世寂寞
死守一世寂寞 2021-01-04 09:43

I\'m currently trying to get my head around some of the things I can do with variadic template support. Let\'s say I have a function like this -

template <         


        
相关标签:
4条回答
  • 2021-01-04 09:55

    This should work:

    void foo(int);
    
    template<typename ...Args>
    void foo(int first, Args... more)
    {
       foo(first);
       foo(std::forward(more)...);
    }
    
    0 讨论(0)
  • 2021-01-04 10:01

    A variation of the top answer which will reject implicit conversion to int , if that is your preference:

    #include <type_traits>
    
    void foo(int);
    
    template<typename Arg1, typename ...Args>
    void foo(Arg1 first, Args... more)
    {
       static_assert(std::is_same_v<Arg1, int>, "foo called with non-int argument");
       foo(first);
       foo(more...);
    }
    
    0 讨论(0)
  • 2021-01-04 10:03

    I'm currently trying to get my head around some of the things I can do with variadic template support.

    Assuming that you want to experiment with variadic templates, not find any solution to your problem, then I suggest taking a look at the code below:

    #include <iostream>
    
    template<int ...Values>
    void foo2()
    {
        int len = sizeof...(Values);
        int vals[] = {Values...};
    
        for (int i = 0; i < len; ++i)
        {
            std::cout << vals[i] << std::endl;
        }
    }
    
    int main()
    {
        foo2<1, 2, 3, 4>();
    
        return 0;
    }
    

    The difference between foo2 and your foo is that you pass the parameters to foo at runtime, and to foo2 at compilation time, so for every parameter set you use, compiler will generate separate foo2 function body.

    0 讨论(0)
  • 2021-01-04 10:06

    If you know the types before, you can use function overload with std:initializer_list :

    #include <initializer_list>
    #include <iostream>
    
    void foo( std::initializer_list<int> l )
    {
        for ( auto el : l )
            // do something
    }
    
    void foo( std::initializer_list<float> l )
    {
    }
    
    void foo( std::initializer_list<std::string> l )
    {
    }
    
    int main()
    {
        foo( {1, 2, 3, 4 } );
        foo( {1.1f, 2.1f, 3.1f, 4.1f } );
        foo( { "foo", "bar", "foo", "foo" } );
        return 0;
    }
    

    If you use Visual Studio 2012, you might need the Visual C++ Compiler November 2012 CTP.

    EDIT : If you still want to use variadic template, you can do :

    template <int ... Args>
    void foo( )
    {
        int len = sizeof...(Args);
        int vals[] = {Args...};
        // ...
    }
    
    // And
    
    foo<1, 2, 3, 4>();
    

    But you have to remember that it is not working with float and std::string for example : you will end with 'float': illegal type for non-type template parameter. float is not legal as a non-type template parameter, this is to do with precision, floating point numbers cannot be precisely represented, and the likelyhood of you referring to the same type can depend on how the number is represented.

    0 讨论(0)
提交回复
热议问题