Reputation: 11416
I'd like to create a type trait which detects the existence of a nested class template within some other type of interest.
For example, suppose I want to create the type trait has_foo
, which detects the presence of a nested template of one parameter named foo
within some type T
:
#include <cassert>
template<class T>
struct has_foo
{
// XXX what goes here?
};
struct with_foo
{
template<class T> struct foo {};
};
struct without_foo {};
int main()
{
assert(has_foo<with_foo>::value);
assert(!has_foo<without_foo>::value);
return 0;
}
What is the best way to implement has_foo
?
Upvotes: 2
Views: 565
Reputation: 61009
template <template<class> class> using void_templ = void;
template <typename, typename=void> struct has_foo : std::false_type {};
template <typename T>
struct has_foo<T, void_templ<T::template foo>> : std::true_type {};
Demo. GCC will allow any type here; That is a bug. To fix that make void_templ
a class template
template <template<class> class>
struct void_templ {using type = void;};
Demo.
Upvotes: 6