fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. class foo
  7. {
  8. public:
  9. int bar;
  10. };
  11.  
  12. template <class C, C>
  13. class Check;
  14.  
  15. template <class T, class Enable = void>
  16. class enable_if_has_bar
  17. {};
  18.  
  19. template <class T>
  20. class enable_if_has_bar<T, Check <decltype(&T::bar),&T::bar>>
  21. {
  22. public:
  23. typedef decltype(static_cast<T*>(0)->*static_cast<decltype(&T::bar)>(0)) type;
  24. };
  25.  
  26. template <class T>
  27. typename enable_if_has_bar<T>::type ReturnBar (const T& value)
  28. {
  29. return value.bar;
  30. }
  31.  
  32. int main ()
  33. {
  34. foo foobar;
  35. foobar.bar = 42;
  36.  
  37. cout << ReturnBar(foobar) << endl;
  38. return 0;
  39. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:20:57: error: 'decltype (& T:: bar)' is not a valid type for a template constant parameter
prog.cpp:20:60: error: template argument 2 is invalid
prog.cpp: In function 'int main()':
prog.cpp:37:26: error: no matching function for call to 'ReturnBar(foo&)'
stdout
Standard output is empty