I am trying to overload operator T()
with SFINAE to return a copy when T
is a fundamental type, and a const reference when T
is a class.
When using double
in my example below, I cannot remove the second overload (using std::is_class
).
That is, I get a message:
error: no type named 'type' in 'struct std::enable_if<false, const double&>' operator typename std::enable_if< std::is_class<T>::value, const T&>::type () const ^
What am I doing wrong?
#include <iostream> #include <type_traits> template<typename T> struct Foo { operator typename std::enable_if<!std::is_class<T>::value, T >::type () const { return _val; } operator typename std::enable_if< std::is_class<T>::value, const T&>::type () const { return _val; } T _val; }; int main() { Foo<double> f1; f1._val = 0.3; double d = f1; std::cout << d << std::endl; return 0; }
source share