[bind] using bind for << to ostream

Hello, i want to use bind to create a pair and forward it to an std::ostream, i.e. like: std::for_each(vec.begin(), vec.end(), bind(&write, std::cout, bind(constructor<std::pair<value_t, value_t> >(), _1, _1))); but the compiler says: ios_base.h:781: error: 'ios_base(const ios_base &)' is private boost/boost/lambda/detail/bind_functions.hpp:472: error: within this context boost/boost/lambda/detail/function_adaptors.hpp: In static member function `static void boost::lambda::function_adaptor< void (*)(ostream &, const pair<value_t, value_t> &) >::apply( void (*)(ostream &, const pair<value_t, value_t> &), const ostream &, const pair<value_t, value_t> &)' ... it seem that temporary is created? why? how to avoid it? i've tried the obvious way (at least for me): std::for_each(vec.begin(), vec.end(), (std::cout << bind(constructor<std::pair<value_t, value_t> >(), _1, _1))); but now the output from compiler is kind of 'heavy stuff' and i'm having difficulties to figure out what am i doing wrong. can you help me, please? many thanks for your attention, Mojmir --------8<---- complete source ------ #include <boost/lambda/bind.hpp> #include <boost/lambda/construct.hpp> #include <memory> #include <vector> #include <iostream> using namespace boost::lambda; typedef int value_t; typedef std::vector<value_t> vect_t; typedef std::vector<std::pair<value_t, value_t> > vect2_t; std::ostream & operator<< (std::ostream & os, std::pair<value_t, value_t> const & p) { return os; } void write (std::ostream & os, std::pair<value_t, value_t> const & p) { } int main () { vect_t vec; vect2_t vec2; vec.push_back(1); std::transform(vec.begin(), vec.end(), std::back_inserter(vec2), bind(constructor<std::pair<value_t, value_t> >(), _1, _1)); // ok /* std::for_each(vec.begin(), vec.end(), bind(&write, std::cout, bind(constructor<std::pair<value_t, value_t> >(), _1, _1)) ); std::for_each(vec.begin(), vec.end(), (std::cout << bind(constructor<std::pair<value_t, value_t> >(), _1, _1))); */ } This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.

Hi Mojmir, On 20/06/07, Mojmir Svoboda <mojmir.svoboda@logicacmg.com> wrote:
Hello,
i want to use bind to create a pair and forward it to an std::ostream, i.e. like:
std::for_each(vec.begin(), vec.end(), bind(&write, std::cout, bind(constructor<std::pair<value_t, value_t> >(), _1, _1)));
but the compiler says: ios_base.h:781: error: 'ios_base(const ios_base &)' is private boost/boost/lambda/detail/bind_functions.hpp:472: error: within this context boost/boost/lambda/detail/function_adaptors.hpp: In static member function `static void boost::lambda::function_adaptor< void (*)(ostream &, const pair<value_t, value_t> &) >::apply( void (*)(ostream &, const pair<value_t, value_t> &), const ostream &, const pair<value_t, value_t> &)' ... it seem that temporary is created? why? how to avoid it?
Have you tried: std::for_each(vec.begin(), vec.end(), bind(&write, boost::ref(std::cout), // !! bind(constructor<std::pair<value_t, value_t> >(), _1, _1))); ? That stops bind taking a copy of std::cout, which should fix the first problem. -- Darren

That's it. Many thanks, Darren!
std::for_each(vec.begin(), vec.end(), bind(&write, boost::ref(std::cout), // !! bind(constructor<std::pair<value_t, value_t> >(), _1, _1)));
That stops bind taking a copy of std::cout, which should fix the first problem.
This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.

Mojmir Svoboda <mojmir.svoboda <at> logicacmg.com> writes: I was intrigued by this problem, but I couldn't solve it myself. I think it has to do with the tuple (user defined types, overloaded operator<<, Bll). consider: typedef int value_t; typedef std::vector<value_t> vect_t; typedef std::pair<value_t, value_t> value_t_pair; typedef std::vector<value_t_pair> vect2_t; value_t make_value_t (value_t v) { return v; } value_t_pair make_value_t_pair(value_t v) { return std::make_pair(v, v); } std::ostream& operator<<(std::ostream& ros, const value_t_pair&) { return ros; } void Foo() { vect_t vec; //compiles std::for_each(vec.begin, vec.end(), std::cout << boost::lambda::bind(&make_value_t, boost::lambda::_1)); //does not compile std::for_each(vec.begin, vec.end(), std::cout << boost::lambda::bind(&make_value_t_pair, boost::lambda::_1)); //does also not compile std::for_each(vec.begin, vec.end(), std::cout << boost::lambda::bind<value_t_pair> (&make_value_t_pair, boost::lambda::_1)); } But maybe a Bll guru can give a better answer.

* gast128 <gast128@hotmail.com> [2007-06-26 08:37:50 +0000]:
Mojmir Svoboda <mojmir.svoboda <at> logicacmg.com> writes:
I was intrigued by this problem, but I couldn't solve it myself. I think it has to do with the tuple (user defined types, overloaded operator<<, Bll). ...
But maybe a Bll guru can give a better answer.
and where can i possibly find one? :) on boost@lists.boost.org for perhaps? many thanks for trying, gast. mojmir This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.
participants (3)
-
Darren Garvey
-
gast128
-
Mojmir Svoboda