Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-03-28 17:25:04


AMDG

Michael Fawcett <michael.fawcett <at> gmail.com> writes:

>
> Steven, do you think it would be easy enough to provide a reciprocal
> helper for when conversions are that straightforward (I'm not familiar
> enough in this domain to know how many instances there are where the
> conversion is not)?
>
> E.g. currently you have to do:
>
> template<>
> struct base_unit_converter<length_tag,nautical::system_tag,SI::system_tag>
> {
> typedef double type;
> static type value() { return 1.852e3; }
> };
>
> template<>
> struct base_unit_converter<length_tag,SI::system_tag,nautical::system_tag>
> {
> typedef double type;
> static type value() { return 1.0/1.852e3; }
> };
>
> Just defining the first one should be enough, and the second one
> should implicitly be the reciprocal of the first (but still give the
> user the option to explicitly say otherwise, of course).

We just make the primary template:

struct define_reverse_automatially {};

struct undefined_conversion {};

template<class Converter>
struct reverse_conversion {
  typedef typename Converter::type type;
  static type value() {
    return(1/Converter::value());
  }
};

template<class Dimension, class Tag1, class Tag2>
struct base_unit_converter :
  mpl::eval_if<is_same<Tag1, Tag2>,
    mpl::identity<trivial_conversion>,
    mpl::if_<
      is_base_and_derived<
        define_reverse_automatially,
        base_unit_converter<Dimension,Tag2,Tag1>
>,
      reverse_conversion<base_unit_converter<Dimension,Tag2,Tag1> >,
      undefined_conversion
>
>::type {};

The you can write

template<>
struct base_unit_converter<length_tag,nautical::system_tag,SI::system_tag>
  : define_reverse_automatically {
    typedef double type;
    static type value() { return 1.852e3; }
};

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk