help with boost/numeric/operators.hpp
I'm haveing some problems figuring out how to use boost/numeric/operators library. OK - I understand how to use template<class T> struct my_type : boost::numeric::operators<T> { T & operator+=(T & rhs); // automatically generate operator+, etc // ... }; so that one can use my_type<int> x, y, z; z = x + y; // etc.. There is a simple example like this in the documentation. So far so good. Now given: int & operator+=(int & lhs, my_type<int>); I want to use operators<T, U> to generate int operator+(int & lhs, my_type<int>); // etc. After studying the documenation, examples and code, the best I can come up with is: template<class T, class U> class helper : public boost::operators<T, U>{ }; T operator+(T & lhs, U & rhs){ helper<T, U>::operator+(lhs, rhs); // automatically generated + operator } // other non member operators forwarding to helper Am I doing this corrrectly? How did the authors of operators.hpp intend the two argument versions to be used? Robert Ramey
Robert Ramey <ramey <at> rrsd.com> writes:
template<class T> struct my_type : boost::numeric::operators<T> { T & operator+=(T & rhs); // automatically generate operator+, etc // ... };
so that one can use my_type<int> x, y, z; z = x + y; // etc..
I'm not familiar with boost::numeric::operators<T> but I have used Boost.Operators to do similar. In this case I'd write: template<typename T> struct my_type : boost::addable<my_type<T> > { my_type& operator+=(my_type<T> const& rhs); ... };
There is a simple example like this in the documentation. So far so good.
Now given:
int & operator+=(int & lhs, my_type<int>);
I want to use operators<T, U> to generate
int operator+(int & lhs, my_type<int>);
If you want that exact return value ("int" not "my_type<int>") then I suppose you've defined the proper operator+= overload for it and just need that visible and to modify my_type as follows: template<typename T> struct my_type : boost::addable<my_type<T>, boost::addable<int, my_type<T> > > { ... }; HTH, -Ryan
participants (2)
-
Robert Ramey
-
Ryan Gallagher