|
Boost : |
Subject: Re: [boost] [proto] Problems with operator <<
From: Eric Niebler (eric_at_[hidden])
Date: 2011-08-24 15:37:12
On 8/24/2011 2:07 PM, John Maddock wrote:
> Folks,
>
> I'm having no end of problems with operator<< and proto.
>
> In the beginning, operator << was overloaded for stream output and
> everything worked OK, then I added the left shift operator<< to the list
> of protoized operators in the grammar and everything broke. The strange
> thing is that:
>
> std::cout << a+b;
>
> correctly finds my stream out overload:
>
> template <class Exp>
> inline std::ostream& operator << (std::ostream& os, const
> big_number_exp<Exp>& r)
>
> But
>
> std::cout << a;
>
> no longer calls:
>
> template <class Backend>
> inline std::ostream& operator << (std::ostream& os, const
> big_number<Backend>& r)
>
> But calls the proto << operator instead.
I'm guessing that big_number<T> inherits from
big_number_exp<proto::terminal<T>::type>. Did I guess right?
I'm also guessing that in "std::cout << a", that a is a non-const
big_number. Did I guess right?
> I'm at a complete loss to explain this, both of my overloads are in the
> same namespace as their respective types, and as far as I can see both
> should be preferred to the proto versions.
>
> Before I start tearing my hair out, anyone any ideas?
Yes. It's picking proto's right-shift operator because "a" is non-const.
It's preferred because your operator<< requires a cv conversion.
Aside: "a+b" returns a const-qualified rvalue, assuming it's using
Proto's operator+. So no problem there.
So, one solution would be to just add a stream inserter (and extractor,
if desired) for non-const big_numbers.
You'll still have problems, though. Try:
std::stringstream str;
str << a;
Now, the LHS isn't *exactly* std::ostream&, and you're back where you
started. Guessing. If that's the case, let me know and I'll help you
modify your grammar appropriately.
-- Eric Niebler BoostPro Computing http://www.boostpro.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk