Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal number of template and function params

Is there a way to generate a templated function where there is an equal number of template params and function params (of the same type)?

Like the following:

// template function:
template<typename ... Args>
void foo(const std::string& a, const std::string& b, ...) { /*...*/}

// example 1
foo<int>("one argument only");

// example 2
foo<int, double>("first argument", "second argument");

What if there is a default function param (not templated) (or multiple) which always exists?

template<typename ... Args>
void foo(int i /* as default */, const std::string& a,
                                 const std::string& b, ...)
 { /*...*/}

Would it even be possible to have, let's say the duplicate number of function arguments (calculated by the size of the template params)?

like image 213
Felix Avatar asked Jun 09 '18 21:06

Felix


People also ask

What is the difference between a function template and a parameter pack?

In a function template, there are no restrictions on the parameters that follow a default, and a parameter pack may be followed by more type parameters only if they have defaults or can be deduced from the function arguments.

How do you use not with equals in a template?

The following example template uses not with equals. Checks whether the first value is greater than the second value. In Bicep, use the > operator instead. See Greater than >. The first value for the greater comparison. The second value for the greater comparison. Returns True if the first value is greater than the second value; otherwise, False.

How to add two numbers using function templates in C++?

C++ Function Template 1 Defining a Function Template. A function template starts with the keyword template followed by template parameter (s)... 2 Calling a Function Template. We can then call it in the main () function to add int and double numbers. 3 Example: Adding Two Numbers Using Function Templates. More ...

What is a template parameter?

In the definition of a member of a class template that appears outside of the namespace containing the class template definition, the name of a template parameter hides the name of a member of this namespace.


1 Answers

Is there a way to generate a templated function where there is an equal number of template params and function params (of the same type)?

Yes: it is (Edit: simplified following a Justin's suggestion; thanks!)

#include <string>

template <typename, typename T>
using skip_first = T;

template <typename ... Args>
void foo (skip_first<Args, std::string> ... as)
 { }

int main()
 {
   foo<int>("one argument only");

   foo<int, double>("first argument", "second argument");
 }

What if there is a default function param (or multiple)?

template<typename ... Args>
void foo(int i /* as default */, const std::string& a,
                                 const std::string& b, ...)
 { /*...*/}

If you want one (or more) preceding parameter(s) of different type(s), yes it's possible.

template <typename ... Args>
void foo (int i, long j, skip_first<Args, std::string> ... as)
 { }

If you want a preceding parameter with a default value, no: isn't possible because a variadic list of parameter can't have a default value and because given a parameter with a default value all fallowing parameters have to have a default parameter.

Would it even be possible to have, let's say the duplicate number of function arguments (calculated by the size of the template params)?

I suppose you can multiply (repeat the unpack more times) the unpack part

template <typename ... Args>
void foo (skip_first<Args, std::string> ... as1,
          skip_first<Args, std::string> ... as2)
 { }

Obviously this solution works only if you want multiply the number of template argument for an integer number.

like image 170
max66 Avatar answered Oct 03 '22 14:10

max66