Can I make a variable template accept a certain type of argument

In C ++ templates, there is a concept with an explicit specifier of template arguments, which means that I can force the compiler to create a template function of this type. eg.

template <class T1, class T2>

void foo(T1 t1prm, T2 t2prm) {
    /* function body */
}

foo<double, double>(1,2); 


Can I do something similar with the function of the variation template? I do not have a specific use case. Variadic templates are new to me, and I'm just trying to understand the possibilities of a new (well, for me) concept.

+4
source share
1 answer

Yes.

template<typename... Args>
void f(const Args&... args)
{
}

int main()
{
   f<int>(1.0, 2, 3.0);
}

function will be called here f<int, int, double>.

You can check it like

template<typename... Args>
void f(const Args&... args)
{
   using swallow = int[];
   (void)swallow{0, (std::cout << typeid(args).name() << std::endl, 0)...};
}
+6
source

All Articles