C++ Templates and Excedrin Migrane

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


#include 
#include 

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 );
    func();
}

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

    Are you abusing bind to simulate a closure?

    try:
    bind( hello, count )();

  2. 2 Mark

    Bheesh,

    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.

    -Mark

  3. 3 Bheeshmar

    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?

    Bheesh

  1. 1 mark++ » Blog Archive » Functors
  2. 2 mark++ » Blog Archive » boost::function

Leave a Reply




Creative Commons Attribution-NonCommercial 3.0 United States
Creative Commons Attribution-NonCommercial 3.0 United States