473,695 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_fun ction<A, B, void>
{
public:
void operator()(cons t A&, const B&) const { }
};

class BadFunctor : public std::binary_fun ction<A, B, void>
{
public:
void operator()(cons t A&, B&) const { }
};
class KludgeFunctor : public std::unary_func tion<A, void>
{
B& b;
public:
KludgeFunctor(B & b_) : b(b_) { }
void operator()(cons t A&) const { }
};

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

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

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

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

Jul 22 '05 #1
1 3375
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_fun ction<A, B, void>
{
public:
void operator()(cons t A&, const B&) const { }
};

class BadFunctor : public std::binary_fun ction<A, B, void>
{
public:
void operator()(cons t A&, B&) const { }
};
class KludgeFunctor : public std::unary_func tion<A, void>
{
B& b;
public:
KludgeFunctor(B & b_) : b(b_) { }
void operator()(cons t A&) const { }
};

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

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

// this generates an error
std::for_each(v .begin(),
v.end(),
std::bind2nd(Ba dFunctor(), 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<_Oper ation>
bind2nd(const _Operation& __fn, const _Tp& __x)
{
typedef typename _Operation::sec ond_argument_ty pe _Arg2_type;
return binder2nd<_Oper ation>(__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
1933
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 template instantiation errors. My class definition is like this,
3
2668
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
3659
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, const T& rhs ) { std::transform( lhs.begin() , lhs.end() , lhs.begin() , std::bind2nd( std::divides<T>(), rhs ) );
4
2640
by: Ioannis Vranos | last post by:
Shouldn't this compile? #include <vector> #include <algorithm> #include <functional> int main() {
4
3245
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
420
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 to do this? Here is a concrete example: If I have a string with sequences of CRLF and possibly just CR's, is there a simple way of replacing the CRLF characters with a LF? If you look at the function below is it possible to use a replace_if...
6
3234
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 std::search_n alot to find a particular value in a range of memory. I'm fairly pleased with std::search_n - in that my benchmark ( below ) shows on average an 8 second performance gain comparable to a for loop. The question. Since I haven't quite...
2
1907
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 reference type. I guess that is because the std::tolower<>() template takes a reference to a locale as its second parameter then bind2nd tries to take the reference of that parameter. But how to I circumvent this?
3
2368
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
2744
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 method, operator () that does the comparison. Fine, no problem. What they fail to tell me is, what is the advantage to wrapping some comparison function in a class and calling it operator()? I also read that "functors" are supposed to be a...
0
9113
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8976
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8830
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8820
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7655
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5837
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4339
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2997
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.