Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Units] quantity<unit> problem with template specialisation algorithm?
From: Thomas Shorrock (Thomas.Shorrock_at_[hidden])
Date: 2010-05-13 10:14:43


Hello,

many thankyous to the developers of Boost.Units and Boost libraries generally,
I have been using them for a few years now and this is my first difficulty.

I have been having some problems when using template specialisation (g++ 4.4.3) with Boost.Units (Boost 1_42_0).
When calling a specialised template function I have found that I need to be very careful with the argument,

In short,
specialised_templated_function(1.0 * boost::units::si::metre) works fine
specialised_templated_function(1 * boost::units::si::metre) specialised function is not found (default tried)

This caught me out for a while and so I thought I would report it in case it is a small bug.
If this is not a Boost issue then my appologies.

In detail, here is the code to reproduce:

#include <iostream>
#include <boost/units/systems/si.hpp>

namespace bu = boost::units;

/** Convert arbitrary units into seconds
 * When a distance is provided, the speed parameter is used to calculate the time.
 * Otherwise the speed variable is ignored.
 */
template<class unit>
bu::quantity<bu::si::time>
any2second(const unit& t,
  const bu::quantity<bu::si::velocity>& speed
    =1*bu::si::meters_per_second)
{
  return bu::quantity<bu::si::time>(t);
}

/* Specialisations for quantity<length> */
template<>
bu::quantity<bu::si::time>
any2second(const bu::quantity<bu::si::length>& d,
           const bu::quantity<bu::si::velocity>& speed)
{
  return bu::quantity<bu::si::time>(d / speed);
}

int main()
{
  //The following use the default function and work fine
  std::cout<<"any2second(1.0*s) = "<<any2second(1.0*bu::si::second)<<std::endl;
  std::cout<<"any2second(1*s) = "<<any2second(1*bu::si::second)<<std::endl;

  //The following uses the specialised function and works fine
  std::cout<<"any2second(1.0*m) = "<<any2second(1.0*bu::si::metre, 1*bu::si::meters_per_second)<<std::endl;
  
  //The following tries to use the defualt function rather than the specialisation.
  //It does not compile because (correctly) a metre cannot be converted into a second.
  //std::cout<<"any2second(1*m) = "<<any2second(1*bu::si::metre, 1*bu::si::meters_per_second)<<std::endl;
  
  return 0;
}

Many thanks,

Tom

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the addressee only. If the message is received by anyone other than the addressee, please return the message to the sender by replying to it and then delete the message from your computer and network.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net