fork(2) download
  1. #include <iostream>
  2. struct GetValueImpl
  3. {
  4. template <typename T>
  5. static auto test(int) -> decltype(
  6. std::declval<T&>().GetValue(0),
  7. std::true_type{});
  8. template <typename...>
  9. static std::false_type test(...);
  10. };
  11. template <typename T>
  12. struct GetValueDefined : public decltype(GetValueImpl::test<T>(0)) {};
  13.  
  14. template <typename Derived, int X>
  15. class Base
  16. {
  17. public:
  18.  
  19. // I want to define this function only if GetValue is defined in Derived
  20. template <typename... Args>
  21. auto Test(const Args&...) -> typename std::enable_if<GetValueDefined<Derived>::value>::type
  22. {
  23. }
  24. };
  25.  
  26. template <std::int32_t val>
  27. class DerivedExample : public Base<DerivedExample<val>, 1>
  28. {
  29. public:
  30. template <typename T>
  31. int GetValue() {return 0;}
  32. };
  33.  
  34. int main() {
  35. DerivedExample<1> d;
  36. d.Test(1, 2);
  37. return 0;
  38. }
  39.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘class Base<DerivedExample<1>, 1>’:
prog.cpp:27:7:   required from ‘class DerivedExample<1>’
prog.cpp:35:23:   required from here
prog.cpp:21:10: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
     auto Test(const Args&...) -> typename std::enable_if<GetValueDefined<Derived>::value>::type
          ^
prog.cpp: In function ‘int main()’:
prog.cpp:36:7: error: ‘class DerivedExample<1>’ has no member named ‘Test’
     d.Test(1, 2);
       ^
stdout
Standard output is empty