Just a question if someone has done it.
I was wondering if (or rather how) I can bind std::cout with boost::bind.
The idea I'm playing around with is to bind an error message to std::cout to reduce code.
I just can't get the syntax right.
This is "closest" I've come to bind it.
boost::bind(&std::operator << < std::char_traits<char> >, std::cout, "Test");
Any ideas (still looking for the proper return type, too)?
Binding overloaded operators is ... tricky, to say the least.
Basically, you need to do an explicit cast in order to let the compiler select the correct overload. That is, you need something like
The whole thing is such a mess that you don't really save any code. Just thing about how often
static_cast< std::ostream& (*)(std::ostream&, const char*) >(&std::operator << <std::char_traits<char> >), std::cout, "Test");
std::cout << msg;
fits into the horrible line above.
A better choice here would be Boost.Lambda:
(ll::ref(std::cout) << "Test")
Assuming you have
namespace ll = boost::lambda
I can see it will be a pain...
I can't seem to find the doc for boost::lambda::ref, though.
Boost's site is so difficult to find things on.
But I'll have to try that and scrap the old idea.
Could be boost::ref. It's a little tool that is used in more than one place, and I don't know where its documentation is.
I found this link. But that isn't for boost::lambda::ref. It is for boost::ref.
Nasty... Is that really going to benefit your program, Elysia?
It's not nasty. It's very powerful and very flexible.
I wonder if other programming languages has anything close to this.
This is a big example where C++ really shines.
Did I say anything inflamatory about what you are doing? Or rather did I simply make a statement as to how your code will end up looking?
I don't know what you meant. What did you mean, because I'm having problems seeing a any problems.
Unless you're talking about bind, which is nasty.
Lambda is way better. Not nasty at all.
Better and certainly not as ugly as bind. But not necessarily visually appealing either.
I see no problems with it what-so-ever.
Indirection might help.
Originally Posted by Elysia
Now this output<T>() is something that is easier to bind. A better name for it is probably in order, though.
template <typename T>
std::ostream &output(std::ostream &stream, const T &val)
return stream << val;
I will take that into account. Thanks.