Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct foo1;
- struct foo2 { typedef int bar; };
- template <typename T>
- class has_bar
- {
- typedef char yes;
- typedef long no;
- template <typename C> static yes check( decltype(&C::bar) ) ;
- template <typename C> static no check(...);
- public:
- enum { value = sizeof(check<T>(0)) == sizeof(yes) };
- };
- int main()
- {
- std::cout << has_bar<foo1>::value << std::endl;
- std::cout << has_bar<foo2>::value << std::endl;
- return 0;
- }
- #include <iostream>
- struct foo1;
- struct foo2 { typedef int bar; };
- template <typename T, typename U = void>
- struct target
- {
- target()
- {
- std::cout << "default target" << std::endl;
- }
- };
- template<typename T>
- struct target<T, typename T::bar>
- {
- target()
- {
- std::cout << "specialized target" << std::endl;
- }
- };
- int main()
- {
- target<foo1>();
- target<foo2>();
- return 0;
- }
- template <typename C> static yes check( decltype(&C::bar) ) ;
- template <typename C> static yes check( typename C::bar* ) ;
- // General utility: if_<Condition, Then, Else>::type
- // Selects 'Then' or 'Else' type based on the value of
- // the 'Condition'
- template <bool Condition, typename Then, typename Else = void>
- struct if_ {
- typedef Then type;
- };
- template <typename Then, typename Else>
- struct if_<false, Then, Else > {
- typedef Else type;
- };
- template <typename T, typename _ = void>
- struct target {
- // generic implementation
- };
- template <typename T>
- struct target<T, typename if_<false,typename T::bar>::type> {
- // specialization for types holding a nested type `T::bar`
- };
- template<class T>
- struct Void {
- typedef void type;
- };
- template<class T, class U = void>
- struct has_bar {
- enum { value = 0 };
- };
- template<class T>
- struct has_bar<T, typename Void<T::bar>::type > {
- enum { value = 1 };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement