How to fix this:
template<class T>
struct ResultType
{
using type = std::conditional_t<std::is_class_v<T>, typename T::result_type, void>;
};
It cannot be it is supposed to return void, if T is not class type, but instead:
error: ‘int’ is not a class, struct, or union type 24 | using type = std::conditional_tstd::is_class_v<T, typename T::result_type, void>;
So I need to not try to invoke the false expression, but how?
In the following:
using type = std::conditional_t<std::is_class_v<T>, typename T::result_type, void>;
The part typename T::result_type will fail when T = int, because typename int::result_type is ill-formed.
You can fix this by using a template specialization instead of std::conditional which does the exact same thing but avoids doing T::result_type when T is not a class type:
#include <type_traits>
template <typename T, typename = void>
struct ResultType;
template <typename T>
struct ResultType<T, std::enable_if_t<!std::is_class_v<T>>> {
using type = void;
};
template<typename T>
struct ResultType<T, std::enable_if_t<std::is_class_v<T>>> {
using type = typename T::result_type;
};
// ...
struct X {
using result_type = int;
};
int main() {
static_assert(std::is_same_v<typename ResultType<X>::type, typename X::result_type>, "FAIL!");
static_assert(std::is_same_v<typename ResultType<int>::type, void>, "FAIL!");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With