Re: STL Template question
So long story short, is there any way to declare variable C in such a way
as to where I don't need to know ahead of time what unit type it's going
to be? I suspect no as I currently can't imagine a way that would be
possible...but since I'm still new to STL I figured I'd ask.
No there isn't. Type of a variable must be known at compile time. If I
understand your problem correctly, the best you can do is the following
(assuming that user also entered unit information as a string):
string userUnit = ...
if (userUnit == "Mil")
// Compute and store Scalar<Mil>
else if (userUnit == "Inch")
// Compute and store Scalar<Inch>
else if (...)
// ...etc...
To store the value in a single variable you can use boost::any:
boost::any scalar;
if (userUnit == "Mil")
scalar = Scalar<Mil>(3);
else if (userUnit == "Inch")
scalar = Scalar<Inch>(5);
Note how boost::any gracefully accomodates value of any type. But you still
need the if statements to create scalar of appropriate type. It's also
possible to replace ugly ifs with a more general "factory" for scalars
(untested code):
// Function to create scalar from double
typedef boost::any (*ScalarFactory)(double);
// Template to generate such functions for all unit types you may have
template<class Unit> boost::any CreateScalar(double v)
{
return boost::any(Scalar<Unit>(v));
}
// Register all unit types you have in the map
map<string, ScalarFactory> factoriesByUnit;
factoriesByUnit.insert(make_pair("Mil", &CreateScalar<Mil>));
factoriesByUnit.insert(make_pair("Inch", &CreateScalar<Inch>));
....etc for all supported units
// Now use the factory to create scalar with appropriate unit
double userValue = ...; // Contains value entered by user
string userUnit = ...; // Contains unit entered by user
boost::any scalar = factoriesByUnit[userUnit](userValue);
// Now scalar contains value with appropriate unit.
// Use any_cast to extract the value (but again you must know the unit :-),
// so another map may be necessary)
cheers,
Marcin