Reputation: 1551
I need a dispatcher function, something like below
template<typename T>
T dispatcher() {
// if T is double
return _func_double();
// if T is int
return _func_int();
// if T is char*
return _func_char_pointer();
}
and will be used like below
// some code that use above dispatcher
template<typename T1, typename T2, typename T3>
void do_multiple_thing(T1 *a1, T2 *a2, T2 *a3) {
*a1 = dispatcher<T1>();
*a2 = dispatcher<T2>();
*a3 = dispatcher<T3>();
}
Could you tell me how to achieve that?
P.S.
- solution for builtin types only suffices.
- both preprocessing and template appoach is acceptable.
Upvotes: 4
Views: 1826
Reputation: 29
If you need a generic solution for writing such dispatchers, something along the lines of this can be used:
// calls the first function that has return type `T` with no arguments
template <class T, class F, class... Fs>
constexpr T call_by_return_type(F&& f, Fs&&... fs) {
if constexpr (std::is_same_v<T, std::invoke_result_t<F>>) {
return f();
} else if constexpr (sizeof...(fs) > 0) {
return call_by_return_type<T>(std::forward<Fs>(fs)...);
} else {
static_assert(
sizeof(T) == 0,
"`T` must match at least one function's return type"
);
}
}
And then you can create dispatchers as a combination of functions (can be any function-object that is called with no arguments):
template <class T>
constexpr T dispatcher() {
return call_by_return_type<T>(
_func_double,
_func_int,
_func_char_pointer
);
}
Note: I assumed you already have _func_<return-type>
functions that need to be grouped to form a dispatcher, otherwise I could think of more elegant interfaces.
Upvotes: 1
Reputation: 1344
If you have compiler with C++17 support this snippet of code should work:
template<typename T>
T dispatcher() {
// if T is double
if constexpr (std::is_same<T, double>::value)
return _func_double();
// if T is int
if constexpr (std::is_same<T, int>::value)
return _func_int();
// if T is char*
if constexpr (std::is_same<T, char*>::value)
return _func_char_pointer();
}
Otherwise you will have to do template specialization, and make overload for each of parameters that you want
//only needed for static assert
template<typename T>
struct always_false : std::false_type {};
template<typename T>
T dispatcher()
{
//to make sure that on type you didn't overload you will have exception
throw std::exception("This type was not overloaded")
//static assert that will provide compile time error
static_assert(always_false<T>::value , "You must specialize dispatcher for your type");
}
//or to get compile time error without static assert
template<typename T>
T dispatcher() = delete; //the simplest solution
template<>
double dispatcher<double>()
{
return _func_double();
}
//... and so on for all other functions
Upvotes: 6
Reputation: 217398
In C++17, you might combine if constexpr and std::is_same
:
template<typename T>
T dispatcher() {
if constexpr (std::is_same<T, double>::value) {
return _func_double();
} else if constexpr (std::is_same<T, int>::value) {
return _func_int();
} else if constexpr (std::is_same<T, char*>::value) {
return _func_char_pointer();
} else {
return {}; // or static_assert(always_false<T>::value); ?
}
}
Before, you might use specialization or tag dispatching with overload:
template<typename T>
T dispatcher() {
return {}; // or static_assert(always_false<T>::value); ?
}
template<>
double dispatcher() {
return _func_double();
}
template<>
int dispatcher() {
return _func_int();
}
template<>
char* dispatcher() {
return _func_char_pointer();
}
or
template<typename T> struct tag {};
template<typename T>
T dispatcher(tag<T>) = delete; // or { return {}; } ?
double dispatcher(tag<double>) { return _func_double(); }
int dispatcher(tag<int>) { return _func_int(); }
char* dispatcher(tag<char*>) { return _func_char_pointer(); }
// some code that use above dispatcher
template<typename T1, typename T2, typename T3>
void do_multiple_thing(T1 *a1, T2 *a2, T2 *a3) {
*a1 = dispatcher(tag<T1>());
*a2 = dispatcher(tag<T2>());
*a3 = dispatcher(tag<T3>());
}
Upvotes: 4
Reputation: 37852
template <typename T>
T fetch_magic_value();
template <>
int fetch_magic_value<int>() { return 23; }
template <>
char fetch_magic_value<char>() { return ' '; }
template <>
std::string fetch_magic_value<std::string>() { return "tada"; }
template<typename T>
void do_multiple_thing(T *x) {
*x = fetch_magic_value<T>();
}
template<typename T, typename... Args>
void do_multiple_thing(T *first, Args *...args)
{
do_multiple_thing(first);
do_multiple_thing(args...);
}
https://wandbox.org/permlink/v2NMhoy8v3q5VhRf
C++17 version: https://wandbox.org/permlink/0pi08jvYF5vlIpud
Upvotes: 3