C++ Templates and Excedrin Migrane

I love C++, really I do, but damn my head hurts.


using namespace std;
using namespace boost;

void hello( int a )
    while( a-- ) cout << "Hi Mom: " << a << endl;
int main()
    const int count = 5;
    /*some type*/ func = bind( hello, count );

The problem is, what the hell should go in place of /*some type*/? I expected void (*)(void), but that doesn’t work.


5 Responses to “C++ Templates and Excedrin Migrane”

  1. 1 Bheeshmar July 24, 2005 at 6:32 pm

    Are you abusing bind to simulate a closure?

    bind( hello, count )();

  2. 2 Mark July 25, 2005 at 9:49 pm


    I’m just trying to bind a parameter now, and call the functor later.

    Can you really simulate a closure with bind? I’m no closure expert, but how is bind( hello, count)(); any different from hello( count ); ?

    The way I see it, the only practical use for bind is to pass it into another function template, such as for_each. That way, the compiler deduces the template parameter, and you don’t have to worry about the complicated type of the bind functor.


  3. 3 Bheeshmar July 26, 2005 at 12:01 pm

    Re: closure simulation.

    What if bind is lexical (that is, does it preserve the value of count or keep a reference)? If it’ s lexical, it’s close enough to my understanding of what a closure is.

    I was trying to make your usage scenario work. You are binding, then calling. I suppose you want to pass the bound functor to a function and that’s why you are trying to deduce the type?


  1. 1 mark++ » Blog Archive » Functors Trackback on July 24, 2005 at 7:07 pm
  2. 2 mark++ » Blog Archive » boost::function Trackback on July 26, 2005 at 11:47 am
Comments are currently closed.

%d bloggers like this: