fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<class Typ>
  5. class base {
  6. public:
  7. const base<Typ> operator+() const{
  8. base<Typ> result;
  9. result.fElement = +fElement;
  10. return result;
  11. }
  12.  
  13. template<typename T>
  14. typename std::enable_if<std::is_same<T, base<double>>::value,
  15. const base<double>>::type
  16. operator^(const T& rgh) const
  17. {
  18. std::cout << fElement << " ^ " << rgh.fElement << '\n';
  19. return *this;
  20. }
  21. private:
  22. Typ fElement;
  23. };
  24.  
  25. typedef base<double> derived;
  26.  
  27. int main(){
  28. derived a, b;
  29. +a; // It compiles
  30. a^b; // It compiles
  31. +a^b; // It compiles
  32. +(a^b); // It compiles
  33.  
  34. base<int> c;
  35. +c; // It compiles
  36. // c^c; // no ^ for base<int>
  37. }
  38.  
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
0 ^ 0
0 ^ 0
0 ^ 0