I am trying to write a class template that uses a parameter-pack and implements a member function for each type contained in the parameter-pack.
This is what I have s
There is no "operator" specifically that supports this, but what you're requesting can be done in a few different ways, depending on your requirements.
The only way to "extract" T
types from a parameter pack of a class template with the purpose of implementing an overload-set of functions is to implement it using recursive inheritance, where each instance extracts one "T" type and implements the function, passing the rest on to the next implementation.
Something like:
// Extract first 'T', pass on 'Rest' to next type
template
class MyClassImpl : public MyClassImpl
{
public:
void doSomething(const T&) { ... }
using MyClassImpl::doSomething;
};
template
class MyClassImpl // end-case, no more 'Rest'
{
public:
void doSomething(const T&) { ... }
};
template
class MyClass : public MyClassImpl
{
public:
using MyClassImpl::doSomething;
...
};
This will instantiate sizeof...(Types)
class templates, where each one defines an overload for each T
type.
This ensures that you get overload semantics -- such that passing an int
can call a long
overload, or will be ambiguous if there are two competing conversions.
However, if this is not necessary, then it'd be easier to enable the function with SFINAE using enable_if
and a condition.
For exact comparisons, you could create an is_one_of
trait that only ensures this exists if T
is exactly one of the types. In C++17, this could be done with std::disjunction
and std::is_same
:
#include
// A trait to check that T is one of 'Types...'
template
struct is_one_of : std::disjunction...>{};
Alternatively, you may want this to only work if it may work with convertible types -- which you might do something like:
template
struct is_convertible_to_one_of : std::disjunction...>{};
The difference between the two is that if you passed a string literal to a MyClass
, it will work with the second option since it's convertible, but not the first option since it's exact. The deduced T
type from the template will also be different, with the former being exactly one of Types...
, and the latter being convertible (again, T
may be const char*
, but Types...
may only contain std::string
)
To work this together into your MyClass
template, you just need to enable the condition with SFINAE using enable_if
:
template
class MyClass
{
public:
// only instantiates if 'T' is exactly one of 'Types...'
template ::value>>
void doSomething(const T&) { ... }
// or
// only instantiate if T is convertible to one of 'Types...'
template ::value>>
void doSomething(const T&) { ... }
};
Which solution works for you depends entirely on your requirements (overload semantics, exact calling convension, or conversion calling convension)
Edit: if you really wanted to get complex, you can also merge the two approaches... Make a type trait to determine what type would be called from an overload, and use this to construct a function template of a specific underlying type.
This is similar to how variant
needs to be implemented, since it has a U
constructor that considers all types as an overload set:
// create an overload set of all functions, and return a unique index for
// each return type
template
struct overload_set_impl;
template
struct overload_set_impl
: overload_set_impl
{
using overload_set_impl::operator();
std::integral_constant operator()(T0);
};
template
struct overload_set : overload_set_impl<0,Types...> {};
// get the index that would be returned from invoking all overloads with a T
template
struct index_of_overload : decltype(std::declval>()(std::declval())){};
// Get the element from the above test
template
struct constructible_overload
: std::tuple_element::value, std::tuple>{};
template
using constructible_overload_t
= typename constructible_overload::type;
And then use this with the second approach of having a function template:
template
class MyClass {
public:
// still accept any type that is convertible
template ::value>>
void doSomething(const T& v)
{
// converts to the specific overloaded type, and call it
using type = constructible_overload_t;
doSomethingImpl(v);
}
private:
template
void doSomethingImpl(const T&) { ... }
This last approach does it two-phase; it uses the first SFINAE condition to ensure it can be converted, and then determines the appropriate type to treat it as and delegates it to the real (private) implementation.
This is much more complex, but can achieve the overload-like semantics without actually requiring recursive implementation in the type creating it.