#include <string>
#include <type_traits>
template<typename T>
struct Point {
using value_type = T;
using self_type = Point<T>;
value_type m_value;
Point(value_type value) : m_value(value) {}
template<typename RhsT,
typename std::enable_if<
std::is_arithmetic<value_type>::value
&& std::is_arithmetic<RhsT>::value, int>::type = 0>
self_type& operator = (const Point<RhsT>& rhs) {
m_value = static_cast<value_type>(rhs.m_value);
return *this;
}
};
int main() {
Point<int> a1(4);
Point<int> a2(5);
Point<double> a3(6.0);
Point<std::string> a4("hello");
a1 = a2;
a2 = a3;
a3 = a4;
return 0;
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHR5cGVfdHJhaXRzPgoKdGVtcGxhdGU8dHlwZW5hbWUgVD4Kc3RydWN0IFBvaW50IHsKCXVzaW5nIHZhbHVlX3R5cGUgPSBUOwoJdXNpbmcgc2VsZl90eXBlID0gUG9pbnQ8VD47CgoJdmFsdWVfdHlwZSBtX3ZhbHVlOwoKICAgIFBvaW50KHZhbHVlX3R5cGUgdmFsdWUpIDogbV92YWx1ZSh2YWx1ZSkge30KCgl0ZW1wbGF0ZTx0eXBlbmFtZSBSaHNULAoJCXR5cGVuYW1lIHN0ZDo6ZW5hYmxlX2lmPAoJCQlzdGQ6OmlzX2FyaXRobWV0aWM8dmFsdWVfdHlwZT46OnZhbHVlCgkJCSYmIHN0ZDo6aXNfYXJpdGhtZXRpYzxSaHNUPjo6dmFsdWUsIGludD46OnR5cGUgPSAwPgoJc2VsZl90eXBlJiBvcGVyYXRvciA9IChjb25zdCBQb2ludDxSaHNUPiYgcmhzKSB7CgkJbV92YWx1ZSA9IHN0YXRpY19jYXN0PHZhbHVlX3R5cGU+KHJocy5tX3ZhbHVlKTsKCQlyZXR1cm4gKnRoaXM7Cgl9Cn07CgppbnQgbWFpbigpIHsKCVBvaW50PGludD4gYTEoNCk7CglQb2ludDxpbnQ+IGEyKDUpOwoJUG9pbnQ8ZG91YmxlPiBhMyg2LjApOwoJUG9pbnQ8c3RkOjpzdHJpbmc+IGE0KCJoZWxsbyIpOwoKICAgIGExID0gYTI7CiAgICBhMiA9IGEzOwogICAgCiAgICBhMyA9IGE0OwoKCXJldHVybiAwOwp9
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>&&'