
Hello, I cannot seem to figure out how to make a composite unit out of two existing units from the same system. For example, say I have a functor F which takes an argument of type F::arg_type = quantity<time> and returns a value of type F::ret_type = quantity<velocity> Now I want to write an integration function, which should return the correct type, something like template<class F> F::ret_type*F::arg_type // only conceptually integrate(F& f, typename F::arg_type a, typename F::arg_type b); so instantiating this with the above functor should produce a return type quantity<time*velocity==length>. How can I combine F::arg_type and F::ret_type to produce the correct return type for the function template? Thank you, Rupert -- Rupert Mazzucco <mazzucco@iiasa.ac.at> Research Scholar, Evolution and Ecology Program IIASA - Institute for Applied Systems Analysis Schlossplatz 1, 2361 Laxenburg, Austria Phone: +43 2236 807 522 Fax: +43 2236 713 13

Hello,
I cannot seem to figure out how to make a composite unit out of two existing units from the same system. For example, say I have a functor F which takes an argument of type
F::arg_type = quantity<time>
and returns a value of type
F::ret_type = quantity<velocity>
Now I want to write an integration function, which should return the correct type, something like
template<class F> F::ret_type*F::arg_type // only conceptually integrate(F& f, typename F::arg_type a, typename F::arg_type b);
so instantiating this with the above functor should produce a return type quantity<time*velocity==length>. How can I combine F::arg_type and F::ret_type to produce the correct return type for the function template?
#include <boost/units/quantity.hpp> #include <boost/units/systems/si.hpp> using namespace boost::units; // functor for velocity as a function of time struct F { typedef quantity<si::time> arg_type; typedef quantity<si::velocity> ret_type; // constant velocity ret_type operator()(arg_type t) const { return 1.0*si::meters/ si::second; } }; template<class F> typename multiply_typeof_helper<typename F::arg_type::unit_type,typename F::arg_type::unit_type>::type integrate(F& f,typename F::arg_type a,typename F::arg_type b) { // put actual integration algorithm here... return F(b)*(b-a); } int main(void) { return 0; } There are corresponding xxx_typeof_helper functions for add, subtract, divide, pow, and root... Of course, if you're using a compiler that fully supports typeof, these shenanigans shouldn't be necessary at all. Matthias

Sorry, I should have actually checked my response... This one works : #include <boost/units/io.hpp> #include <boost/units/quantity.hpp> #include <boost/units/systems/si.hpp> #include <iostream> using namespace boost::units; // functor for velocity as a function of time struct F { typedef quantity<si::time> arg_type; typedef quantity<si::velocity> ret_type; // constant velocity ret_type operator()(const arg_type& t) const { return 1.0*si::meters/ si::second; } }; template<class F> typename multiply_typeof_helper<typename F::ret_type,typename F::arg_type>::type integrate(F& f,typename F::arg_type a,typename F::arg_type b) { // put actual integration algorithm here... return f(b)*(b-a); } int main(void) { F f; std::cout << integrate(f,0.0*si::seconds,1.0*si::seconds) << std::endl; }

On Fri, 11 Sep 2009 12:05:33 -0600 Matthias Schabel <boost@schabel-family.org> wrote:
Sorry, I should have actually checked my response... This one works :
Thank you very much, this is exactly what I was looking for. Bye, Rupert -- Rupert Mazzucco <mazzucco@iiasa.ac.at> Research Scholar, Evolution and Ecology Program IIASA - Institute for Applied Systems Analysis Schlossplatz 1, 2361 Laxenburg, Austria Phone: +43 2236 807 522 Fax: +43 2236 713 13
participants (2)
-
Matthias Schabel
-
Rupert Mazzucco