Here is another working solution that you can use for this.
The basic idea is to rely on function overloading and sfinae to send a request and avoid multiple definitions.
This follows a minimal working example:
#include<type_traits> #include<utility> #include<cassert> template <typename T1, typename T2> typename std::enable_if<std::is_same<T1, int>::value or std::is_same<T2, int>::value, int>::type determineImpl(int, T1 a, T2 b) { return 42; } template <typename T1, typename T2> typename std::enable_if<not std::is_same<T1, int>::value and not std::is_same<T2, int>::value, double>::type determineImpl(char, T1 a, T2 b) { return .42; } template<typename... Args> auto determine(Args&&... args) -> decltype(determineImpl(0, std::forward<Args>(args)...)) { return determineImpl(0, std::forward<Args>(args)...); } int main() { auto v1 = determine(42, 'c'); assert(v1 == 42); auto v2 = determine('c', .42); assert(v2 == .42); }
source share