fork(2) download
  1. #include <string>
  2. #include <type_traits>
  3.  
  4. template<typename T>
  5. struct Point {
  6. using value_type = T;
  7. using self_type = Point<T>;
  8.  
  9. value_type m_value;
  10.  
  11. Point(value_type value) : m_value(value) {}
  12.  
  13. template<typename RhsT,
  14. typename std::enable_if<
  15. std::is_arithmetic<value_type>::value
  16. && std::is_arithmetic<RhsT>::value, int>::type = 0>
  17. self_type& operator = (const Point<RhsT>& rhs) {
  18. m_value = static_cast<value_type>(rhs.m_value);
  19. return *this;
  20. }
  21. };
  22.  
  23. int main() {
  24. Point<int> a1(4);
  25. Point<int> a2(5);
  26. Point<double> a3(6.0);
  27. Point<std::string> a4("hello");
  28.  
  29. a1 = a2;
  30. a2 = a3;
  31.  
  32. a3 = a4;
  33.  
  34. return 0;
  35. }
Compilation error #stdin compilation error #stdout 0s 3136KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:32:8: error: no match for 'operator=' (operand types are 'Point<double>' and 'Point<std::basic_string<char> >')
     a3 = a4;
        ^
prog.cpp:32:8: note: candidates are:
prog.cpp:17:13: note: template<class RhsT, typename std::enable_if<(std::is_arithmetic<double>::value && std::is_arithmetic<_Tp>::value), int>::type <anonymous> > Point<T>::self_type& Point<T>::operator=(const Point<RhsT>&) [with RhsT = RhsT; typename std::enable_if<(std::is_arithmetic<_Tp>::value && std::is_arithmetic<RhsT>::value), int>::type <anonymous> = <enumerator>; T = double]
  self_type& operator = (const Point<RhsT>& rhs) {
             ^
prog.cpp:17:13: note:   template argument deduction/substitution failed:
prog.cpp:16:53: error: no type named 'type' in 'struct std::enable_if<false, int>'
    && std::is_arithmetic<RhsT>::value, int>::type = 0>
                                                     ^
prog.cpp:16:53: note: invalid template non-type parameter
prog.cpp:5:8: note: Point<double>& Point<double>::operator=(const Point<double>&)
 struct Point {
        ^
prog.cpp:5:8: note:   no known conversion for argument 1 from 'Point<std::basic_string<char> >' to 'const Point<double>&'
prog.cpp:5:8: note: Point<double>& Point<double>::operator=(Point<double>&&)
prog.cpp:5:8: note:   no known conversion for argument 1 from 'Point<std::basic_string<char> >' to 'Point<double>&&'
stdout
Standard output is empty