Topic: can binary binders take a modifiable argument?


Author: Scott Meyers <usenet@aristeia.com>
Date: Sat, 24 Oct 2009 20:18:37 CST
Raw View
Jeff Flinn wrote:
>
> Daniel Kr   gler wrote:
>>
>> for_each(..., bind(&C::f, 3));
>
> You meant of course:
>
> for_each(..., bind(&C::f, _1, 3));

bind's horrific syntax, while a worthy generalization of bind1st and
bind2nd, is one reason I believe that lambdas are almost always a
better bet:

 for_each(..., [](C& obj){ obj.f(3); });

Lambdas also have the advantage over bind that because function
pointers are not involved, I expect compilers will be much more likely
to inline calls made via them e.g., C::f in the above.

Scott


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Al Grant <algrant@myrealbox.com>
Date: Wed, 14 Oct 2009 12:37:16 CST
Raw View
Do binder1st and binder2nd have one operator() that takes
a reference-to-const?  Or do they have another operator()
that takes a plain reference?

The published C++ standard, and also
http://stdcxx.apache.org/doc/stdlibref/bind1st.html
suggest there is only one operator().  But GNU STL has both,
and so does VC++, as documented at
http://msdn.microsoft.com/en-us/library/12zw43tk(VS.71).aspx
Is this an official change or a non-standard extension?
I can't see anything in the library issues lists.

Having both operators gives a bit more flexibility, e.g. you can
iterate over a sequence of C's invoking a member function
'f' that modifies the object (taking an argument):

 for_each(..., bind2nd(mem_fun_ref(&C::f), 3));

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Fri, 16 Oct 2009 11:44:08 CST
Raw View
On 14 Okt., 20:37, Al Grant <algr...@myrealbox.com> wrote:
> Do binder1st and binder2nd have one operator() that takes
> a reference-to-const?  Or do they have another operator()
> that takes a plain reference?

This depends on the reference document, see below.

> The published C++ standard, and alsohttp://stdcxx.apache.org/doc/stdlibref/bind1st.html
> suggest there is only one operator().  But GNU STL has both,
> and so does VC++, as documented athttp://msdn.microsoft.com/en-us/library/12zw43tk(VS.71).aspx
> Is this an official change or a non-standard extension?
> I can't see anything in the library issues lists.

In C++03 there is one operator() in those binders
standardized, which accepts an argument of type
const typename Operation::second_argument_type&
or const typename Operation::first_argument_type&, resp.

The current working paper for the upcoming C++0x
standard suggests the following changes:

1) Add one further operator() overload which takes
as argument typename Fn::second_argument_type&,
or typename Fn::first_argument_type&, resp.

2) Both binder1st and binder2nd are deprecated, the
recommended replacement is bind, which has a more
intuitive using syntax and supports much more
use-cases.

> Having both operators gives a bit more flexibility, e.g. you can
> iterate over a sequence of C's invoking a member function
> 'f' that modifies the object (taking an argument):
>
>  for_each(..., bind2nd(mem_fun_ref(&C::f), 3));

Yes, this is one of the reasonable use-cases for this
additional overload. Using std::bind this can be written
as

for_each(..., bind(&C::f, 3));

which is even simpler ;-)

HTH & Greetings from Bremen,

Daniel Kr   gler


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jeff Flinn <TriumphSprint2000@hotmail.com>
Date: Fri, 16 Oct 2009 13:05:28 CST
Raw View
Daniel Kr   gler wrote:
On 14 Okt., 20:37, Al Grant <algr...@myrealbox.com> wrote:
Do binder1st and binder2nd have one operator() that takes
a reference-to-const?  Or do they have another operator()
that takes a plain reference?

This depends on the reference document, see below.

The published C++ standard, and
alsohttp://stdcxx.apache.org/doc/stdlibref/bind1st.html
suggest there is only one operator().  But GNU STL has both,
and so does VC++, as documented
athttp://msdn.microsoft.com/en-us/library/12zw43tk(VS.71).aspx
Is this an official change or a non-standard extension?
I can't see anything in the library issues lists.

In C++03 there is one operator() in those binders
standardized, which accepts an argument of type
const typename Operation::second_argument_type&
or const typename Operation::first_argument_type&, resp.

The current working paper for the upcoming C++0x
standard suggests the following changes:

1) Add one further operator() overload which takes
as argument typename Fn::second_argument_type&,
or typename Fn::first_argument_type&, resp.

2) Both binder1st and binder2nd are deprecated, the
recommended replacement is bind, which has a more
intuitive using syntax and supports much more
use-cases.

Having both operators gives a bit more flexibility, e.g. you can
iterate over a sequence of C's invoking a member function
'f' that modifies the object (taking an argument):

 for_each(..., bind2nd(mem_fun_ref(&C::f), 3));

Yes, this is one of the reasonable use-cases for this
additional overload. Using std::bind this can be written
as

for_each(..., bind(&C::f, 3));

You meant of course:

for_each(..., bind(&C::f, _1, 3));


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]