So I have this really ugly code:
template
std::conditional_t
I had to do something like this once so I wrote a small wrapper to acheive the result neatly. You could use it as follows (see here for a test)
template
typename static_switch
,static_case
,static_case
>::type foo(T bar){ ... }
Behind the scenes it pretty much does what you already have but by wrapping it we keep it (more) readable. There is also a version to allow you to switch direclty on the type T if you needed that.
Edit: At @Deduplicator's suggestion here is the code behind it
#include
/*
* Select a type based on the value of a compile-time constant such as a
* constexpr or #define using static_switch.
*/
template
struct static_case {
static constexpr int value = I;
using type = T;
};
template
struct static_switch{
using type = typename std::conditional< I==Case1::value ,
typename Case1::type,
typename static_switch::type
>::type;
};
struct fail_on_default {};
template
struct static_switch {
using type = typename std::conditional< I==LastCase::value ,
typename LastCase::type,
DefaultType
>::type;
static_assert(!(std::is_same::value),
"Default case reached in static_switch!");
};