[Boost.Units] quantity<unit> problem with template specialisation algorithm?
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.
AMDG Thomas Shorrock wrote:
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)
1 * boost::units::si::metre returns boost::units::quantity<boost::units::si::length, int>, which doesn't match boost::units::quantity<boost::units::si::lenth, double>. You would have the same problem if you used built-in types directly. In Christ, Steven Watanabe
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Steven Watanabe Sent: Thursday, May 13, 2010 6:46 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Boost.Units] quantity<unit> problem with template specialisation algorithm?
AMDG
Thomas Shorrock wrote:
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)
1 * boost::units::si::metre returns boost::units::quantity<boost::units::si::length, int>, which doesn't match boost::units::quantity<boost::units::si::lenth, double>. You would have the same problem if you used built-in types directly.
As ever, Steven is quite right about this, but I have also plopped right into this pit myself in my first fumbles with Boost.Units. It could certain use a bit louder warning in the docs - I'm sure I won't be the last to make this mistake. Is there a reason why int isn't 'supported' (for built-in types at least)? Is it to avoid potential bloat (short, long, unsigned ..)? Or just too much hassle? Paul --- Paul A. Bristow Prizet Farmhouse Kendal, UK LA8 8AB +44 1539 561830, mobile +44 7714330204 pbristow@hetp.u-net.com
participants (3)
-
Paul A. Bristow
-
Steven Watanabe
-
Thomas Shorrock