Ideally, we can do something like this with enable_if:
#include <type_traits>
namespace detail {
enum class enabler_t { DUMMY };
}
template<bool B>
using enable_if_u = typename std::enable_if<B, detail::enabler_t>::type;
template<bool B>
using disable_if_u = typename std::enable_if<!B, detail::enabler_t>::type;
template<typename T, enable_if_u<std::is_same<T, int>::value>...>
int a(){return 0;}
template<typename T, disable_if_u<std::is_same<T, int>::value>...>
double a(){return 0.0;}
int main() {
auto x = a<int>();
}
Which, imho, is the best way to use it.
However, this does not work with Clang due to error 11723 .
The workaround I'm using is using the constexpr variable:
namespace detail {
enum class enabler_t { DUMMY };
constexpr const enabler_t dummy = enabler_t::DUMMY;
}
template<typename T, enable_if_u<std::is_same<T, int>::value> = detail::dummy>
int a(){return 0;}
template<typename T, disable_if_u<std::is_same<T, int>::value> = detail::dummy>
double a(){return 0.0;}
I am not a big fan of this workaround. I was wondering if there is another way to work around code in Clang? I am looking for a clean solution in C ++, without using a preprocessor.
The example itself is here to illustrate the problem, it is not useful at all, and there are tons of ways to do it better. I just want to find a more suitable solution for the enable_if part.