AMDG On 09/20/2013 10:34 AM, david.hagood@gmail.com wrote:
I'm looking at the Boost::Units classes to help eliminate a family of programming errors, but there is one thing that I'm having difficulty with:
I have a collection of objects of my own type that pertains to units - these are of a fixed type, with instances per type of unit.
I'd like to somehow be able to map from the units constants that Boost uses (e.g. boost:units:si:watts) into my objects (return the correct instance for "watts"). Obviously, there's going to be some hand coding to make that happen - it is unlikely to be possible to generate that mapping automatically.
Ideally, what I'd like to do would be to have something like
template <typename Unit> struct MyMapper { static const MyUnitInfo info; };
template <> const MyUnitInfoboost::units::si::watts::info(/*ctor parms for my type */);
and do things like:
MyMapperboost::units::si::watts::info
to get the appropriate structure.
However, since all the various Boost units are in a type hierarchy to express dimensionality, I'm at a loss how to compose a template to allow something like that to happen - it certainly isn't as straightforward as the above, since Boost units instances of varying types, not typenames.
I don't quite understand why this doesn't work. (Other than the obvious typos.) I also have no idea what you mean by "varying types, not typenames"
Likewise, just defining some form of map:
std::map??,MyUnitInfo> mymap = boost::assign::map_list_of (boost::units::si::watts,MyUnitInfo(/* my parms*/)) (boost::units::si::ohms,MyUnitInfo(...));
won't work as the types for the Boost constants are all different as far as I can see.
fusion::map can handle this.
Is there some base type all the Boost::Units constants extend, such that I could test for that?
No. If you want to identify units and quantities use the metafunctions is_unit and is_quantity. In Christ, Steven Watanabe