Boost logo

Boost Users :

Subject: Re: [Boost-users] plus<boost::gregorian::date> - how to?
From: Julian Gonggrijp (j.gonggrijp_at_[hidden])
Date: 2011-03-20 17:27:43


qplace wrote:

> I need to be able to either add or substruct boost::gregorian::days from
> boost::gregorian::date.
>
> As far as I understand I have to define
> plus<boost::gregorian::date>
> and minius<boost::gregorian::date>
> and provide function operators for them.
>
> My question is:
> Function operator required in plus and minus template has a form of
> T operator()(const T& x, const T& y)
> , but I need to use not only gregorian::date, but gregorian::days as well.

Sometimes it's perfectly reasonable to allow adding operands of different
types, for example also when incrementing a random access iterator. In such
a case you can just use parameters of different types and keep the rest of
the semantics the same. Like this:

namespace bg = boost::gregorian;

bg::date operator+ (const bg::date&, const bg::days&);
bg::date operator- (const bg::date&, const bg::days&);

and perhaps also

bg::date operator+ (const bg::days&, const bg::date&);
bg::date operator- (const bg::days&, const bg::date&);

You can't use std::plus and std::minus in this way, but you only need those
when the operation has to be passed as a function object to another
function.

HTH, Julian


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