考虑下面的例子
template<class Type = void> class MyClass
{
public:
double getValue()
{
// if "Type == void" return _x, if "Type != void" return _y
return (/* SOMETHING */) ? (_x) : (_y);
}
protected:
double _x;
static const double _y;
};
可能是什么/* SOMETHING */
健康)状况 ?
我想回来_x
如果模板参数为void,则返回_y
如果不。怎么做 ?
First off, you can't return anything because the function return type is void
.(fixed)
其次,您可以专门化该函数以在以下情况下采取不同的行为:Type
is void
:
template<class Type> class MyClass
{
public:
double getValue()
{
return _y;
}
protected:
double _x;
static const double _y;
};
template<>
inline double MyClass<void>::getValue()
{
return _x;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)