I would like to write something like this:
template <class T>
using MyTypeOrTupple = typename std::conditional<has_member_type_MyType<T>::value,
typename T::MyType,
std::tuple<> >::type;
I implemented has_member_type_MyType
using https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector
However, GCC (4.8.4) still complains about using T::MyType
when MyType
is not defined in T
. Is there a way to solve this?
Do not use that crazy thing from Wikibooks.
template <class T>
using MyType_t = typename T::MyType;
template<class T>
using MyTypeOrTuple = detected_or_t<std::tuple<>, MyType_t, T>;
Where detected_or_t
is std::experimental::detected_or_t
from library fundamentals TS v2, and can be implemented as follows:
namespace detail {
template<class...> struct voidify { using type = void; };
template<class...Ts> using void_t = typename voidify<Ts...>::type;
template <class Default, class AlwaysVoid,
template<class...> class Op, class... Args>
struct detector {
using value_t = std::false_type;
using type = Default;
};
template <class Default, template<class...> class Op, class... Args>
struct detector<Default, void_t<Op<Args...>>, Op, Args...> {
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
template <class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;
template< class Default, template<class...> class Op, class... Args >
using detected_or_t = typename detected_or<Default, Op, Args...>::type;
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