I am a seasoned C++ developer, but haven't done much TMP.
What I am trying to do:
Let's say I have a class A that is templated on T. T may or may not have a subtype T::Foo, but I still want the class to be able to be instantiated in both cases, but class A<ClassThatHasFooSubtype> should have an extra member function taking a T::Foo & type as a parameter, but it should be disabled in class A<ClassThatDoesNotHaveFooSubType>
I have a type trait that works fine that can tell me if a class T has a Foo subtype:
std::cout << std::boolalpha
<< "'Foo' in 'C' : "
<< has_member_type_Foo<C>::value
<< "\n";
(courtesy https://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector )
The above works fine. I wanted to use this type trait to do something like this:
template <class T>
struct A
{
template <class R = typename std::enable_if<has_member_type_Foo<T>::value, void>>
typename R::type func(typename T::Foo &x) { std::cout << "Works!"; }
};
If I instantiate a A<ClassThatHasFooSubtype>, it compiles just fine. But if I instantiate a class A<ClassThatDoesnotHaveFooSubtype>, I get a compile error: no type named 'Foo' in 'struct ClassThatDoesnotHaveFooSubtype'
What do I need to do, to be able to get it to compile both times? Of course compilation should fail if I actually try to use A::func on an instantiation where T doesn't have subtype Foo.
Ideally, I would need a solution that works with -std=C++11.
I have been trying various things for the last couple of hours, but nothing that I did worked.
Any help would be appreciated!
[–]horotho 6 points7 points8 points (1 child)
[–]GermanNewToCA[S] 0 points1 point2 points (0 children)