上下文:
我们正在尝试建立一个名为Operand的类模板,它可以将几种类型作为其类型名称T.这些在以下枚举中定义:
enum eOperandType {
INT8
INT16,
INT32,
FLOAT,
DOUBLE
};
Run Code Online (Sandbox Code Playgroud)
那些对应于定义的类型<cstdint>,即int8_t, int16_t,等等.
构造函数必须是Operand(std::string const & value);.
template<class T>
class Operand : public IOperand
{
public:
Operand(std::string const & value)
{
std::stringstream ss(value);
ss >> _value;
//_type = ??? ;
}
[...]
private:
Operand(void){}
eOperandType _type;
T _value;
};
Run Code Online (Sandbox Code Playgroud)
接口IOperand在这里并不重要,只是运算符重载的一些原型.
问题:
设置_type属性的最佳方法是什么?最简单的办法是只写几个if/else if用typeid或接近的东西,但我觉得这将是非常脏.此外,我只是认为typeid在模板内部使用只是意味着你在某处做错了什么......对吗?