C++ Variadic Function Templates of Known Type

*爱你&永不变心* 提交于 2019-11-30 02:10:32

问题


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 <typename ... Args>
void foo(Args ... a)
{
    int len = sizeof...(tail);
    int vals[] = {a...};
    /* Rest of function */
}

/* Elsewhere */
foo(1, 2, 3, 4);

This code works because I assume beforehand that the arguments will be integers, but obviously will fail if I provide something else. If I know that the parameter packs will contain a particular type in advance, is there some way that I can do without the templating and have something like -

void foo(int ... a)

I have tried doing this, but the compiler gave an error about foo being a void field. I know that I can also access the parameters in the pack through recursion, but I'm not sure this will fix the problem that I have - namely I want to be able to take a variable number of arguments of the same type.


回答1:


This should work:

void foo(int);

template<typename ...Args>
void foo(int first, Args... more)
{
   foo(first);
   foo(std::forward(more)...);
}



回答2:


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.




回答3:


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.




回答4:


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...);
}


来源:https://stackoverflow.com/questions/17792051/c-variadic-function-templates-of-known-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!