Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange boost::bind behavior (compile)

Reply
Thread Tools

Strange boost::bind behavior (compile)

 
 
Kira Yamato
Guest
Posts: n/a
 
      01-28-2008
#include <iostream>
#include "boost/bind.hpp"

using namespace std;
using namespace boost;

int sum3(int x, int y, int z) { return x+y+z; }

int sum2(int x, int y) { return x+y; }

int sum1(int x) { return x; }

int main()
{
// Let's try some standard example uses of boost::bind first.

// Ex 1: prints 1.
cout << (bind(sum1, _1))(1) << endl;

// Ex 2: prints 11.
cout << (bind(sum2, _1,_2))(1,10) << endl;

// Both Ex 1 and Ex 2 works as advertised. However,

// Ex 3: (won't compile!)
cout << (bind(sum3, _1,_2,_3))(1,10,100) << endl;

// The above Ex 3 fails to compile. What did I do wrong?

// Let's try some nested uses of boost::bind below.

// Ex 4: prints 11.
cout << (bind(sum2, bind(sum1, _1), bind(sum1, _2)))(1,10) << endl;

// Ok, Ex 4 is functionally equivalent to Ex 2.
// But it's still cool to see that placeholders can span across bind's.
// Hmm. I wonder if I can span placeholders arbitrarily. So,
I attempted ...

// Ex 5: (won't compile!)
cout << (bind(sum2, bind(sum2, _1,_2), bind(sum2,
_3,_4)))(1,10,100,1000) << endl;

// I guess I was asking for too much.
// The resulting bind object will not accept 4 arguments.
// However, when I tried ...

// Ex 6: prints 22.
cout << (bind(sum2, bind(sum2, _1,_2), bind(sum2,
_1,_2)))(1,10) << endl;

// So, I hypothesize that boost looks at the leading bind
object to determine
// the maximum number of arguments to accept.
// In this case, the leading bind object was sum2(int, int).
// So, it only accepts at most 2 arguments.

// If my hypothesis is right, then I expect the following to
*not* compile:

// Ex 7: prints 11.
cout << (bind(sum1, bind(sum2, _1,_2)))(1,10) << endl;

// The leading bind is sum1(int) taking 1 argument.
// But Ex 7 still compiles and runs just fine, even though I
passed to it 2 arguments.
// At this point, I'm somewhat confused.

// My question to this newsgroup is this:
// Does anyone know the precise rule on
// how boost::bind figures out how many arguments to accept?

// Note: I am using g++4.0.1 and boost 1.34.1.

return 0;
}

//--

//-kira

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thunderbird strange behavior... Jim Firefox 5 11-17-2005 03:09 PM
Firefox 1.04 and Strange Find Behavior Thomas Firefox 5 06-28-2005 08:40 PM
utf8 pragma - strange behavior ryang Perl 1 04-11-2005 05:38 AM
strange behavior when using 'read' sstark Perl 0 03-06-2005 02:27 AM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments