fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<typename T>
  5. class ptr {
  6. T* m_ptr;
  7. public:
  8. ptr(T* a_ptr) : m_ptr(a_ptr) {}
  9.  
  10. template<typename U = T>
  11. typename
  12. std::enable_if<!std::is_same<U, void>::value, U&>::type
  13. operator * () const { return *m_ptr; };
  14. };
  15.  
  16. int main()
  17. {
  18. int i = 12345;
  19. ptr<int> ptr_i(&i);
  20. std::cout << *ptr_i; // OK
  21.  
  22. ptr<void> ptr_i2(&i);
  23. std::cout << *ptr_i2; // ERROR
  24.  
  25. return 0;
  26. }
Compilation error #stdin compilation error #stdout 0s 4364KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:23:15: error: no match for ‘operator*’ (operand type is ‘ptr<void>’)
  std::cout << *ptr_i2; // ERROR
               ^~~~~~~
prog.cpp:13:3: note: candidate: ‘template<class U> typename std::enable_if<(! std::is_same<U, void>::value), U&>::type ptr<T>::operator*() const [with U = U; T = void]’
   operator * () const { return *m_ptr; };
   ^~~~~~~~
prog.cpp:13:3: note:   template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template<class U> typename std::enable_if<(! std::is_same<U, void>::value), U&>::type ptr<void>::operator*<U>() const [with U = void]’:
prog.cpp:23:16:   required from here
prog.cpp:13:3: error: forming reference to void
stdout
Standard output is empty