STL Template question

S

Stephan Rose

Hi everyone,

I have a little problem I'm not sure what to do about or if anything can
even be done about it.

Situation is as follows:

I had previous implemented a class called Scalar that is essentially a
wrapper around double that contains an additional field to store what
unit type is represented by this double.

So this allowed me to add 2 values together regardless of their unit
type. For example, 1 inch + 1 millimeter. The way I had implemented this
was just using one class, with a small 2 dimensional table that contained
the conversion factors. Then the various arithmetic operators would do a
lookup in this conversion table using the unit type as index into the
table.

The result works very well, but has a significant performance overhead
compared to just plain double. It pretty much cuts the speed in half.

Also, another problem with this table is that I have the factor "1" going
across the diagonal. So everytime I am performing arithmetic with units
of the same type I have a needless multiply by 1 in there.

This was before I started using STL. =)

Using STL however I came up with a completely different concept that
eliminates the conversion table and the need to store the unit type.

I defined a template class called "Scalar", and then for each unit type,
I also defined an additional class. All classes implement the appropriate
arithmetic operators and the unit type classes contain function that
allow them to convert to other types of units. The Scalar template then
contains conversion operators that will then call the appropriate
conversion function of it's type when necessary.

The benefit of this is that if I add 1 mil to 1 mil, there no longer is a
needless multiply by 1. It is now reduced to 1 double + 1 double and
that's it. Zero performance overhead for adding units of the same type.

If I add units of different type, the conversion operator in the Scalar
template just calls the appropriate conversion function of it's type and
then performs the arithmetic. Advantage here being that the conversion is
now much simpler than before as it now is just a static multiply by a
constant and no longer involves a table lookup. Not as fast as adding
just 2 doubles, but still by magnitudes faster than my previous method.

Also, my memory footprint is significantly reduced as I no longer need to
store a unit type.

So essentially, my code went from this:

Scalar a(1, ScalarUnitMil);
Scalar b(1, ScalarUnitInch);
Scalar c = a + b;

To this:

Scalar<Mil> a(1);
Scalar<Inch> b(1);
Scalar<Mil> c = a + b;

Now, here is where my problem comes in. Scalar c in my old code is unit
independent. The unit type that it will be depends on the result of a +
b; So if I add inch to an inch then c will be an inch. If I add a
millimeter to a millimeter it'll be a millimeter.

With my STL version however, I have to explicitly state what unit I'm
going to store in it. So no matter what my math operation is, the result
will be Mil.

99% of the time, this isn't a problem. It's a problem 1% of the time when
the user goes to enter data and I then go to store that value. The user
input is going to be converted to whatever unit type I previously defined
in my code and I will loose that information. The value of course will
still be correct, but it'll be in a different unit. This isn't a major
deal-breaker, but if I could avoid it then it would be nice.

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.

Thanks,

--
Stephan
2003 Yamaha R6

å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰
 
M

Marcin Kalicinski

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
 
G

Gianni Mariani

Stephan Rose wrote:
....
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.

I think you need a proxy object.

There is already an open source library that does what you're trying to
do. I can't remember it's name.

Using a proxy object means you don't actually return another Scalar<T>
but an ScalarOperate<T,U,Add> and AddScalar has a conversion operator
that works out what you want. The problem of course is that you need to
supply 4 more operators for each binary operator.

If you do find the unit library I'm referring to, do post a link.

This is one library - I'm pretty sure I was thinking of somthing different.
http://enbridge.kundert.2y.net/units/doc/
 
S

Stephan Rose

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

Ahh thank you for the info Marcin.

I'll definitely need to take a closer look at boost then!

Much appreciated,

--
Stephan
2003 Yamaha R6

å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰
 
J

Juha Nieminen

Stephan said:
This was before I started using STL. =)

Not directly related to your question, but...

I think you have a misunderstanding about what "STL" means.
"STL" is the old name of the standard C++ library (consisting
of a big bunch of template classes and functions). The acronym
"STL" means "Standard Template Library". Even though it's
technically not the official name of the standard C++ library,
it's still used today informally, for brevity, to refer to the
collection of template classes and functions found in the standard
C++ libraries.

The term you are referring to is simply "template", not "STL".
In other words, eg. "this was before I started using templates".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top