[operator] Can't figure out syntax

Assuming I have a class like the following:
template <typename T>
struct point : boost::additive

Peter, AFAIK operator += or operator -= signature must contain a return value. In your case it can probably be void. template<class U> void operator+=(U offset) { ... } ... Best Regards, Ovanes On Fri, Apr 25, 2008 at 10:41 AM, Peter
Assuming I have a class like the following:
template <typename T> struct point : boost::additive
{ T x_; T y_; explicit point(T x=0, T y=0) : x_(x), y_(y) { }
operator += (T offset) { x_ += offset; y_ += offset; }
operator -= (T offset) { x_ -= offset; y_ -= offset; } }
Now I want to change the addition and subtraction operators to allow for different types using the following template
class point ...
template <typename U> operator += (U offset) { x_ += offset; y_ += offset; }
template <typename U> operator -= (T offset) { x_ -= offset; y_ -= offset; }
...
I cannot figure out how to write the proper syntax for the boost::additive class, so that it generates the proper additional operators for all type combinations. Is that possible, at all?
Thanks, Peter
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ovanes,
sorry, I made an error when writing the sample. It should have been:
template <typename T>
struct point : boost::additive

Peter, the short answer is: if U is convertible to T you don't need to overload operators += or -=. U is going to be converted to T at all. The long answer is to think how this feature can be implemented in C++. Let's consider the operator+ implementation for type T. It requires left and right hand values to operate on and returns a new value as a sum of these to. So if for T an operator += is defined we can implement T operator+(T const& lhs, T const& rhs) { T result=lhs; return (result+=rhs); } That's fine so far, but the operator+ is a non-member operator, so how to pass exactly this T type and no other? Since we want only one special type to be added to our point (in sake of type safety and many other reasons). This is possible using the Barton-Nackman Trick: template<class T> struct point { friend point operator+(point<T> const& lhs, T const& rhs) { point<T> result=lhs; return (result+=rhs); } }; And now imagine you define a template version of the operator += on type other then T, why should the operator+ call it if it expects T? That's why it does not work. That's why it does not work. Regards, Ovanes Fri, Apr 25, 2008 at 10:12 PM, Peter
Ovanes,
sorry, I made an error when writing the sample. It should have been:
template <typename T> struct point : boost::additive
{ T x_; T y_; explicit point(T x=0, T y=0) : x_(x), y_(y) { }
point & operator += (T offset) { x_ += offset; y_ += offset; return *this; }
point & operator -= (T offset) { x_ -= offset; y_ -= offset; return *this; } } This part is working as it should.
My problem comes as soon as I make member templates out of the operators, for example like so
... template <typename U> point & operator += (U offset) { x_ += offset; y_ += offset; return *this; } ...
How do I write the boost::additive stuff so that it works with the operator template with all the types U that are instantiated? Is that at all possible?
Peter
participants (2)
-
Ovanes Markarian
-
Peter