Boost logo

Boost Users :

From: Robert Dailey (rcdailey_at_[hidden])
Date: 2008-03-21 23:41:42


On Fri, Mar 21, 2008 at 10:16 PM, Steven Watanabe <watanabesj_at_[hidden]>
wrote:

> AMDG
>
> Robert Dailey wrote:
> > Hi,
> >
> > I have defined a global overloaded operator<<. I want to use this
> > overloaded operator in std::for_each(), but it must be properly
> > deduced since there are many global overloads for this operator. For
> > example:
> > <snip>
> >
> >
> > What am I doing wrong? Do I need to be using boost bind instead? If
> > so, how can I use boost.bind to map a global operator? Will it
> > correctly deduce which overload to use?
>
> Unfortunately, Boost.Lambda does not handle user defined operators
> automatically.
> If you don't do this Lambda will try to return a Stream by value which
> apparently
> triggers SFINAE when Stream is abstract.
>
> #include <iostream>
> #include <boost/lambda/lambda.hpp>
> #include <vector>
> #include <algorithm>
>
> class Stream
> {
> public:
> virtual void abstract() = 0;
> // This is an abstract class (cannot instantiate)
> void write(char ch) {
> std::cout << ch;
> }
> };
>
> class MyStream : public Stream {
> void abstract() {}
> };
>
> Stream& operator<< ( Stream& stream, char data )
> {
> stream.write( data );
> return stream;
> }
>
> namespace boost {
> namespace lambda {
>
> template<>
> struct plain_return_type_2<bitwise_action<leftshift_action>, Stream, char>
> {
> typedef Stream& type;
> };
>
> }
> }
>
> Stream& operator<< ( Stream& stream, std::vector<char>& buffer )
> {
> using namespace boost::lambda;
> std::for_each( buffer.begin(), buffer.end(), var( stream ) << _1 );
> return stream;
> }
>
> int main() {
> MyStream s;
> std::string str("test1");
> std::vector<char> b(str.begin(), str.end());
> s << b;
> }
>
> See http://www.boost.org/doc/html/lambda/extending.html

Will the plain_return_type_2 specialization also work for classes derived
from Stream? Or do I need to specialize that structure for each and every
single class, regardless of inheritance? Thanks.



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