AMDG On 09/21/2013 05:18 AM, David Hagood wrote:
On 09/20/2013 11:11 PM, Steven Watanabe wrote:
(Other than the obvious typos.) I also have no idea what you mean by "varying types, not typenames"
Example: boost::units::si::meter is a constant of type unit
. boost::units:si::watts is a constant of type unit
. Since they are constants, any template would have to be of the form
template
struct mapper { static const MyStruct value; }; However, since unit_t is variant - unit
, unit , etc. - it has to be a template parameter as well - and come before the constant. So now the template would have to be:
template < typename dim_t, typename sys_t, const boost::units::unit
&units> struct mapper { static const MyStruct value; }; But now those paramters have to be specified in the use.
You don't have to make it that complex:
template<class T>
struct mapper;
template<>
sruct mapperboost::units::si::power
{
static const MyStruct value;
};
...
Usage:
mapper
The other approach would be to use a function: template < typename dim_t, typename sys_t> const MyStruct &Map(unit
const &value); which isn't bad on invocation: const MyStruct &x = Map(meters);
but gets more difficult on definition
template <> const MyStruct &Map
(unit const &) { static const MyStruct mymeters(/* parameters */); return mymeters; } template <> const MyStruct &Map
(unit const &) { static const MyStruct mywatts(/* parameters */); return mywatts; } since for every type you have to go look up the full dimensionality.
Don't specialize. Overload.
const MyStruct& Map(si::length);
const MyStruct& Map(si::mass);
const MyStruct& Map(si::power);
...
N.B. si::length = unit
Now, were there some base type for boost::units::unit
class unit_base { };
template
class unit: public unit_base { } Then this whole thing would be simpler, as they my code could use the base type to key off. (I would have to handle the fact there are multiple definitions in the library, e.g. meter/meters/metre/metres).
You shouldn't rely on the object identity. If you really wanted to do that, you could use void* as the key, anyway. In Christ, Steven Watanabe