RE: [boost] boost::ref simple question

The boost::function template works with boost::ref according to the docs, so try boost::function< int ( int ) > ( boost::ref( Op() ) ) HTH, Michiel Salters
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org]On Behalf Of Neal D. Becker Sent: maandag 8 november 2004 16:16 To: boost@lists.boost.org Subject: [boost] boost::ref simple question
This must be a common question.
I want to use std::transform with a function object that is expensive to copy, so I want to pass by reference. I tried to use boost::ref for this, but this simple test doesn't work:
struct Op { int operator() (int x) { return 1 + x;} Op() {} private: Op (const Op&);
};
int main() { vector<int> x (4); vector<int> y (4); Op o; std::transform (x.begin(), x.end(), y.begin(), boost::ref(o)); }
Any suggestion?
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
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.

Michiel Salters wrote:
The boost::function template works with boost::ref according to the docs, so try boost::function< int ( int ) > ( boost::ref( Op() ) )
Thanks. AFAICT, the best solution seems to be another set of algorithms, like this: struct Op { int operator() (int x) { return 1 + x;} Op() {} private: Op (const Op&); }; template<typename in_t, typename out_t, typename func> void transform_r (in_t const& in, out_t& out, func& f) { std::transform<typename boost::range_const_iterator<in_t>::type,typename boost::range_iterator<out_t>::type,func&> (boost::begin (in), boost::end (in), boost::begin (out), f); } int main() { vector<int> x (4); vector<int> y (4); Op o; transform_r (x, y, o); }

On Nov 9, 2004, at 6:45 AM, Michiel Salters wrote:
The boost::function template works with boost::ref according to the docs, so try boost::function< int ( int ) > ( boost::ref( Op() ) )
Or boost::bind: boost::bind(boost::ref(Op()), _1, _2) boost::ref should actually do this on its own (according to Library TR1), but the code to do so is languishing in the sandbox. Doug

Doug Gregor wrote:
On Nov 9, 2004, at 6:45 AM, Michiel Salters wrote:
The boost::function template works with boost::ref according to the docs, so try boost::function< int ( int ) > ( boost::ref( Op() ) )
Or boost::bind:
boost::bind(boost::ref(Op()), _1, _2)
boost::ref should actually do this on its own (according to Library TR1), but the code to do so is languishing in the sandbox.
What needs to be done in order to promote the sandbox ref into a boost ref?

On Nov 10, 2004, at 9:50 AM, Peter Dimov wrote:
Doug Gregor wrote:
On Nov 9, 2004, at 6:45 AM, Michiel Salters wrote:
The boost::function template works with boost::ref according to the docs, so try boost::function< int ( int ) > ( boost::ref( Op() ) ) Or boost::bind: boost::bind(boost::ref(Op()), _1, _2) boost::ref should actually do this on its own (according to Library TR1), but the code to do so is languishing in the sandbox.
What needs to be done in order to promote the sandbox ref into a boost ref?
Dealing with broken compilers, mostly. IIRC, the code is usable and mostly correct (the TR has changed slightly, so it is likely out-of-sync w.r.t. member pointers and such), but we can't have reference_wrapper barfing on all the compilers that don't support its tricks. Doug

Doug Gregor wrote:
On Nov 10, 2004, at 9:50 AM, Peter Dimov wrote:
Doug Gregor wrote:
On Nov 9, 2004, at 6:45 AM, Michiel Salters wrote:
The boost::function template works with boost::ref according to the docs, so try boost::function< int ( int ) > ( boost::ref( Op() ) ) Or boost::bind: boost::bind(boost::ref(Op()), _1, _2) boost::ref should actually do this on its own (according to Library TR1), but the code to do so is languishing in the sandbox.
What needs to be done in order to promote the sandbox ref into a boost ref?
Dealing with broken compilers, mostly. IIRC, the code is usable and mostly correct (the TR has changed slightly, so it is likely out-of-sync w.r.t. member pointers and such), but we can't have reference_wrapper barfing on all the compilers that don't support its tricks.
How about putting result_of in boost and looking at the regression tests as a first step?

On Nov 10, 2004, at 11:17 AM, Peter Dimov wrote:
How about putting result_of in boost and looking at the regression tests as a first step?
result_of is doing quite nicely already, thank you :) http://www.meta-comm.com/engineering/boost-regression/developer/ utility.html Doug

Doug Gregor wrote:
On Nov 10, 2004, at 11:17 AM, Peter Dimov wrote:
How about putting result_of in boost and looking at the regression tests as a first step?
result_of is doing quite nicely already, thank you :)
http://www.meta-comm.com/engineering/boost-regression/developer/ utility.html
Umm... I must have missed it. OK, if result_of works, what doesn't?

On Nov 10, 2004, at 2:40 PM, Peter Dimov wrote:
Doug Gregor wrote:
On Nov 10, 2004, at 11:17 AM, Peter Dimov wrote:
How about putting result_of in boost and looking at the regression tests as a first step? result_of is doing quite nicely already, thank you :) http://www.meta-comm.com/engineering/boost-regression/developer/ utility.html
Umm... I must have missed it. OK, if result_of works, what doesn't?
Just from skimming the source: - References to member pointers don't work (easy to fix) - If we're to support some broken compilers, we'll need a workaround when result_of<...>::type returns void (these compilers might not support result_of anyway, so it may not matter) - reference_wrapper_without_result_type needs a "void operator()() const { return get(); }" - reference_wrapper_without_result_type needs to have the right "result" typedef (not "result_of"; easy fix). I could have written the fixes for those in less time than it took me to write out that list :) I'll try to get the sandbox version fixed, documented, and into the Boost trunk in the next few days. Doug

Doug Gregor <dgregor@cs.indiana.edu> writes:
On Nov 10, 2004, at 2:40 PM, Peter Dimov wrote:
Doug Gregor wrote:
On Nov 10, 2004, at 11:17 AM, Peter Dimov wrote:
How about putting result_of in boost and looking at the regression tests as a first step? result_of is doing quite nicely already, thank you :) http://www.meta-comm.com/engineering/boost-regression/developer/ utility.html
Umm... I must have missed it. OK, if result_of works, what doesn't?
Just from skimming the source:
- References to member pointers don't work (easy to fix) - If we're to support some broken compilers, we'll need a workaround when result_of<...>::type returns void (these compilers might not support result_of anyway, so it may not matter) - reference_wrapper_without_result_type needs a "void operator()() const { return get(); }" - reference_wrapper_without_result_type needs to have the right "result" typedef (not "result_of"; easy fix).
I could have written the fixes for those in less time than it took me to write out that list :) I'll try to get the sandbox version fixed, documented, and into the Boost trunk in the next few days.
Now we need to improve the default implementation on compilers with typeof support and supply a default for the others that uses Arkadiy's typeof emulation, right? -- Dave Abrahams Boost Consulting http://www.boost-consulting.com
participants (5)
-
David Abrahams
-
Doug Gregor
-
Michiel Salters
-
Neal D. Becker
-
Peter Dimov