fork(1) download
  1. #include <iostream>
  2. #include <utility>
  3. #include <tuple>
  4.  
  5. template <class Type>
  6. class HasStringOperator
  7. {
  8. template <typename T, T> struct TypeCheck;
  9.  
  10. typedef char Yes;
  11. typedef long No;
  12. template <typename T> struct operator_{
  13. typedef char (T::*fptr)(int);
  14. };
  15.  
  16. template <typename T> static Yes HasOperator(TypeCheck< typename operator_<T>::fptr, &T::operator[] >*);
  17. template <typename T> static No HasOperator(...);
  18.  
  19. public:
  20. static bool const value = (sizeof(HasOperator<Type>(0)) == sizeof(Yes));
  21. };
  22.  
  23. struct A
  24. {
  25. char operator[](int){}
  26. };
  27. struct B
  28. {};
  29.  
  30. int main()
  31. {
  32. std::cout<<HasStringOperator<A>::value;
  33. std::cout<<HasStringOperator<B>::value;
  34. }
  35.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
10