On Fri, Mar 21, 2008 at 10:16 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
> <snip>
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:
>Unfortunately, Boost.Lambda does not handle user defined operators
>
> 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?
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() {}
};namespace boost {
Stream& operator<< ( Stream& stream, char data )
{
stream.write( data );
return stream;
}
namespace lambda {
template<>
struct plain_return_type_2<bitwise_action<leftshift_action>, Stream, char>
{
typedef Stream& type;
};return stream;
}
}
Stream& operator<< ( Stream& stream, std::vector<char>& buffer )
{
using namespace boost::lambda;
std::for_each( buffer.begin(), buffer.end(), var( stream ) << _1 );
}
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