我正在尝试operator T()使用SFINAE 重载以在T基本类型时返回副本,并在T类时使用const引用.
在double下面的示例中使用a 时,我无法std::is_class删除第二个重载(with ).
也就是说,我得到的错误是:
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
^
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
#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; …Run Code Online (Sandbox Code Playgroud)