|
| 1 | +/* |
| 2 | + This code normally fails to compile under Visual Studio 2012 |
| 3 | +
|
| 4 | + The fix is marked with _MSC_VER |
| 5 | +*/ |
| 6 | + |
| 7 | +#include <iostream> |
| 8 | +#include <typeinfo> |
| 9 | + |
| 10 | +template <class T> struct has_lowest_layer_type { |
| 11 | + typedef char yes; |
| 12 | + typedef struct {char dummy[2];} no; |
| 13 | + template <class C> static yes f(typename C::lowest_layer_type*); |
| 14 | + template <class C> static no f(...); |
| 15 | +#ifdef _MSC_VER |
| 16 | + static const bool value = sizeof(f<T>(0)) == 1; |
| 17 | +#else |
| 18 | + static const bool value = sizeof(has_lowest_layer_type<T>::f<T>(0)) == 1; |
| 19 | +#endif |
| 20 | +}; |
| 21 | + |
| 22 | +template <bool Enable> |
| 23 | +struct EnableIf : std::false_type { }; |
| 24 | + |
| 25 | +template <> |
| 26 | +struct EnableIf <true> : std::true_type { }; |
| 27 | + |
| 28 | +struct tcp { }; |
| 29 | + |
| 30 | +template <class Protocol> |
| 31 | +struct basic_socket |
| 32 | +{ |
| 33 | + typedef basic_socket <Protocol> lowest_layer_type; |
| 34 | +}; |
| 35 | + |
| 36 | +template <class Protocol> |
| 37 | +struct basic_stream_socket : basic_socket <Protocol> |
| 38 | +{ |
| 39 | + typedef basic_socket <Protocol> next_layer_type; |
| 40 | +}; |
| 41 | + |
| 42 | +struct A |
| 43 | +{ |
| 44 | + typedef basic_socket <tcp> lowest_layer_type; |
| 45 | +}; |
| 46 | + |
| 47 | +struct B |
| 48 | +{ |
| 49 | +}; |
| 50 | + |
| 51 | +template <typename T> |
| 52 | +void show (std::true_type) |
| 53 | +{ |
| 54 | + std::cout << typeid(T).name() << " has lowest_layer_type" << std::endl; |
| 55 | +} |
| 56 | + |
| 57 | +template <typename T> |
| 58 | +void show (std::false_type) |
| 59 | +{ |
| 60 | + std::cout << typeid(T).name() << " does not have lowest_layer_type" << std::endl; |
| 61 | +} |
| 62 | + |
| 63 | +template <typename T> |
| 64 | +void show () |
| 65 | +{ |
| 66 | + show <T> (EnableIf <has_lowest_layer_type <T>::value> ()); |
| 67 | +} |
| 68 | + |
| 69 | +int main () |
| 70 | +{ |
| 71 | + show <A> (); |
| 72 | + show <B> (); |
| 73 | + show <basic_socket <tcp> > (); |
| 74 | + show <basic_stream_socket <tcp> > (); |
| 75 | + return 0; |
| 76 | +} |
| 77 | +/* |
| 78 | +1>..\..\Subtrees\beast\notes\1\test.cpp(16): error C2783: 'has_lowest_layer_type<T>::no has_lowest_layer_type<T>::f(...)' : could not deduce template argument for 'C' |
| 79 | +1> with |
| 80 | +1> [ |
| 81 | +1> T=A |
| 82 | +1> ] |
| 83 | +1> ..\..\Subtrees\beast\notes\1\test.cpp(15) : see declaration of 'has_lowest_layer_type<T>::f' |
| 84 | +1> with |
| 85 | +1> [ |
| 86 | +1> T=A |
| 87 | +1> ] |
| 88 | +1> ..\..\Subtrees\beast\notes\1\test.cpp(63) : see reference to class template instantiation 'has_lowest_layer_type<T>' being compiled |
| 89 | +1> with |
| 90 | +1> [ |
| 91 | +1> T=A |
| 92 | +1> ] |
| 93 | +1> ..\..\Subtrees\beast\notes\1\test.cpp(68) : see reference to function template instantiation 'void show<A>(void)' being compiled |
| 94 | +*/ |
0 commit comments