Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-03-21 23:16:47


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

In Christ,
Steven Watanabe


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