Consider the following:
struct A { typedef int foo; }; struct B {}; template<class T, bool has_foo = /* ??? */> struct C {};
I want to specialize C so that C <A> gets one specialization and C <B> gets another, based on the presence or absence of typename T :: foo. Is it possible to use type traits or some other template magic?
The problem is that everything I tried creates a compilation error when instantiating C <B> because B :: foo does not exist. But this is what I want to check!
Edit: I think ildjarn's answer is better, but I finally came up with the following C ++ 11 solution. Man is hacked, but at least short. :)
template<class T> constexpr typename T::foo* has_foo(T*) { return (typename T::foo*) 1; } constexpr bool has_foo(...) { return false; } template<class T, bool has_foo = (bool) has_foo((T*)0)>
c ++ templates typetraits template-specialization
drwowe
source share