fork(4) download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class has_size {
  5. private:
  6. typedef char Yes;
  7. typedef Yes No[2];
  8.  
  9. template <typename U, U> struct really_has;
  10.  
  11. template<typename C> static Yes& Test(really_has <size_t (C::*)() const, &C::size>*);
  12. template<typename C> static Yes& Test(really_has <size_t (C::*)(), &C::size>*);
  13.  
  14. template<typename> static No& Test(...);
  15.  
  16. public:
  17. static bool const value = sizeof(Test<T>(0)) == sizeof(Yes);
  18. };
  19.  
  20. struct NoSize {};
  21.  
  22. struct Sized { size_t size() const { return 0; } };
  23.  
  24. struct MutableSized { size_t size() { return 0; } };
  25.  
  26. struct BadlySized { int size() { return 0; } };
  27.  
  28. struct MultiSized { size_t size() const { return 0; } int size(int) { return 0; } };
  29.  
  30. int main() {
  31. std::cout << "NoSize: " << has_size<NoSize>::value << "\n";
  32. std::cout << "Sized: " << has_size<Sized>::value << "\n";
  33. std::cout << "MutableSized: " << has_size<MutableSized>::value << "\n";
  34. std::cout << "BadlySized: " << has_size<BadlySized>::value << "\n";
  35. std::cout << "MultiSized: " << has_size<MultiSized>::value << "\n";
  36. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
NoSize: 0
Sized: 1
MutableSized: 1
BadlySized: 0
MultiSized: 1