Function Overloading
If you simply want to call parameter packs based on the signature of the passed arguments, you may do it with just a different overloads for the same function:
void a(int x)
{
//do something with x
}
void a(int x, float y)
{
// do something with x and y
}
template<typename... Args>
void dispatcher(Args... args)
{
a(args...);
}
Tag Dispatch
If you would like to select the function based on the value of the boolean, and you will always have 2 arguments (1 int
, 1 float
), you may do it with a template boolean and tag dispatch:
#include <type_traits>
void a(int x)
{
//do something with x
}
void b(int x, float y)
{
// do something with x and y
}
template<bool B, typename... Args>
void disp_impl(std::true_type, Args... args)
{
a(args...);
}
template<bool B, typename... Args>
void disp_impl(std::false_type, Args... args)
{
b(args...);
}
template<bool B, typename... Args>
void dispatcher(Args... args)
{
using type = std::integer_constant<bool, B>;
a(type{}, args...);
}
Runtime
If you need run-time selection, you must do it the old-fashion way. Remember that all function signatures must be valid, since the branch evaluation is not known at compile-time. Therefore, this is only useful if you are passing the same arguments to all versions of the function, and the boolean value is not known at compile-time. This relies on a helper for get
described here.
void a(int x)
{
//do something with x
}
void b(int x, float y)
{
// do something with x and y
}
template<typename... Args>
void dispatcher(bool a_or_b, Args... args)
{
if (a_or_b)
a(get<0>(args...));
else
b(args...);
}