#Templated functions that accept a number of args

12 messages · Page 1 of 1 (latest)

high cape
#

I’ve been getting familiar with templates and I understand them now but when I started looking at example code with templates functions, I’ve seen some where it seems like they are able to accept any number of arguments and I don’t think there creating overloads for versions that take 1 2 3 or 5+ args. How is this done?

sand siloBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

obsidian rune
#

variadic template parameters

#

e.g. ```cpp
template<typename... Args>
void f(Args... args);

#

Args can be a pack of any number of types

#

hi kolio98

lusty yoke
lusty yoke
obsidian rune
#

this might be beyond their understanding, which is why I made a very simple example

lusty yoke
# obsidian rune e.g. ```cpp template<typename... Args> void f(Args... args); ```

to view the raw templates here @high cape you can use:


template<typename... Args>
void f(Args... args)
{
    int num1 = 20;
}

int main()
{
    f(1, 4.5, "text", 0.0f, &main);
}

which shows:

<source>: In instantiation of 'void f(Args ...) [with Args = {int, double, const char*, float, int (*)()}]':
<source>:10:6:   required from here
   10 |     f(1, 4.5, "text", 0.0f, &main);
      |     ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:5:9: error: unused variable 'num1' [-Werror=unused-variable]
    5 |     int num1 = 20;
      |         ^~~~
cc1plus: all warnings being treated as errors
Compiler returned: 1
#

and here the compiler warning shows the template types instantiated inside the function f