I’m implementing a variant class (not using boost) and I’m wondering how you’d handle the case where you’d store any of string, integer, or double and automatically convert it accordingly to desired type through ToString(), ToInt(), or ToDouble().
For example,
Variant a = 7;
cout << "The answer is" + a.ToString() << endl; // should print "The answer is 7"
a = "7.4";
double &b = a.ToDouble();
b += 1;
cout << a.ToString() << endl; // should print 8.4
ToXXX functions should return the reference of the type that you want to convert to. Right now, I have the code where it can return the same type as it was initially assigned to( Variant a = Int(7); a.ToInt() works) and raise exception when the assigned type is different from the one that you want to convert to.
Sorry using boost isn’t an option.
1 Answer