473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3360
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Black | last post by:
Hi, I have some code like this, itr = find_if(this->pool.begin(), this->pool.end(), bind2nd(mem_fun_ref(&Pool::isAvailable), make_pair(base, high))); But compiler always complains a lot STL...
3
by: ES Kim | last post by:
Here's a simple code: #include <vector> #include <algorithm> #include <functional> using namespace std; struct S { void f(int) const { }
4
by: Steven T. Hatton | last post by:
This code works for dividing each element of a boost::array<> by a value of its element type: template <typename T, size_t S> inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs,...
4
by: Ioannis Vranos | last post by:
Shouldn't this compile? #include <vector> #include <algorithm> #include <functional> int main() {
4
by: Eric Lilja | last post by:
Hello, consider this program: #include <iostream> #include <algorithm> #include <string> #include <cctype> int main() { std::string s = "HEJ";
0
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used...
6
by: ma740988 | last post by:
I've been perusing for some time now - I suppose some of the algorithms in Josuttis. 'Today', I do alot of funny math on data resident within memory. As part of this. I find myself using...
2
by: benben | last post by:
I tried to do the following: bind2nd(ptr_fun(&tolower<char>), locale()); But was hit with errors claiming some where deep down the STL library some code is trying to take a reference to a...
3
by: Chris Roth | last post by:
I have a vector (v) containing objects of class C. class C { private: double d; public: void foo( B& b ); };
4
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.