468,720 Members | 1,485 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

std::bind2nd and non-const references

Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call
in the following code. Why can't I use bind2nd for a functor with a non-const
reference as the second parameter? I have to use kludge it by using something like
KludgeFunctor().

---------------------------
#include <vector>
#include <algorithm>
#include <functional>

class A {
public:
A() { }
A(const A&) { }
};

class B {
public:
B() { }
B(const B&) { }
};

class GoodFunctor : public std::binary_function<A, B, void>
{
public:
void operator()(const A&, const B&) const { }
};

class BadFunctor : public std::binary_function<A, B, void>
{
public:
void operator()(const A&, B&) const { }
};
class KludgeFunctor : public std::unary_function<A, void>
{
B& b;
public:
KludgeFunctor(B& b_) : b(b_) { }
void operator()(const A&) const { }
};

void f()
{
std::vector<A> v;
B b;

// this compiles
std::for_each(v.begin(),
v.end(),
std::bind2nd(GoodFunctor(), b));

// this generates an error
std::for_each(v.begin(),
v.end(),
std::bind2nd(BadFunctor(), b));

// this is the workaround for BadFunctor
std::for_each(v.begin(),
v.end(),
KludgeFunctor(b));
}
----------------------------------

Jul 22 '05 #1
1 3170
red floyd wrote:
Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call
in the following code. Why can't I use bind2nd for a functor with a
non-const
reference as the second parameter? I have to use kludge it by using
something like
KludgeFunctor().

---------------------------
#include <vector>
#include <algorithm>
#include <functional>

class A {
public:
A() { }
A(const A&) { }
};

class B {
public:
B() { }
B(const B&) { }
};

class GoodFunctor : public std::binary_function<A, B, void>
{
public:
void operator()(const A&, const B&) const { }
};

class BadFunctor : public std::binary_function<A, B, void>
{
public:
void operator()(const A&, B&) const { }
};
class KludgeFunctor : public std::unary_function<A, void>
{
B& b;
public:
KludgeFunctor(B& b_) : b(b_) { }
void operator()(const A&) const { }
};

void f()
{
std::vector<A> v;
B b;

// this compiles
std::for_each(v.begin(),
v.end(),
std::bind2nd(GoodFunctor(), b));

// this generates an error
std::for_each(v.begin(),
v.end(),
std::bind2nd(BadFunctor(), b));

// this is the workaround for BadFunctor
std::for_each(v.begin(),
v.end(),
KludgeFunctor(b));
}
----------------------------------


std::bind2nd takes a const reference as its second argument; see the
following code from the gcc stl_function.h header. It shouldn't be
difficult to roll your own version of bind2nd to use a non-const reference.

template <class _Operation, class _Tp>
inline binder2nd<_Operation>
bind2nd(const _Operation& __fn, const _Tp& __x)
{
typedef typename _Operation::second_argument_type _Arg2_type;
return binder2nd<_Operation>(__fn, _Arg2_type(__x));
}

Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John Black | last post: by
3 posts views Thread by ES Kim | last post: by
4 posts views Thread by Steven T. Hatton | last post: by
4 posts views Thread by Ioannis Vranos | last post: by
reply views Thread by Andrew Maclean | last post: by
6 posts views Thread by ma740988 | last post: by
2 posts views Thread by benben | last post: by
3 posts views Thread by Chris Roth | last post: by
4 posts views Thread by Christopher | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.