A shorter way to allow a class to class :: typedef

I have several classes. So far, they are separated by one character. Few of them contain type (a typedef ), and some of them do not have it.

 struct A { ... public: typedef someclass type; } struct B { ... }; 

I want to implement the SFINAE class so that

 Resolve<A>::type o1; // should resolve to 'A::type' Resolve<B>::type o2; // should resolve to 'B' 

One way is to use the basic SFINAE, as shown in the previous link, which checks if T contains a type , and then uses the bool checker. For instance,

 template <typename T> struct has_type { typedef char yes[3]; template <typename C> static yes& test(typename C::type*); template <typename> static char& test(...); static const bool value = sizeof(test<T>(0)) == sizeof(yes); }; template<typename TYPE, bool = has_type<TYPE>::value> struct Contains { typedef typename TYPE::type type; }; template<typename TYPE> struct Contains<TYPE, false> { typedef TYPE type; }; template<class TYPE> struct Resolve { typedef typename Contains<TYPE>::type type; }; 

Demo

Question I have many such instances from code, and I feel that this method can significantly increase compilation time. Because you need to go through two iterations: the 1st one to find the type and the 2nd one to allow the bool flag.

Is there a faster way to reduce compilation time?

[Lateral note: in this case, I split type as a separator between A and B However, I can freely put anything inside A that separates it from B Ideas related to this are also welcome.]

+4
source share
1 answer
 template<typename> struct void_ { typedef void type; }; template<typename T, typename = void> struct Resolve { typedef T type; }; template<typename T> struct Resolve <T, typename void_<typename T::type>::type> { typedef typename T::type type; }; 
+3
source

All Articles