Context:
We're trying to set up a class template, named Operand, which could take several types as its typename T
. Those are defined in the following enum:
enum eOperandType {
INT8
INT16,
INT32,
FLOAT,
DOUBLE
};
Those correspond to the types defined in <cstdint>
, that is, int8_t, int16_t
, and so on.
The constructor must be 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;
};
The interface IOperand is nothing important here, just some prototypes for operator overloads.
Question:
What's the best way to set the _type
attribute? The easy way would be to just write several if/else if
with typeid
or something close to that, but I feel like that would be pretty dirty. Furthermore, I just think that using typeid
inside of a template just means that you're doing something wrong somewhere... right?