views:

181

answers:

2

LS,

Let me first explain what I'm trying to achieve using some pseudo-code (JavaScript).

// Declare our function that takes a callback as as an argument, and calls the callback with true.
B(func) { func(true); }

// Call the function
B(function(bool success) { /* code that uses success */ });

I hope this says it all. If not, please comment on my question so I can write a little more to clarify my issue.

What I want is to have code like this in C++.

I have tried to use lambda functions, but I was unable to specify a parameter type for those.

Kind regards,

Matthias Vance

+2  A: 

EDIT: Upon reading your question again, it looks like you might be looking for anonymous functions in C++. If that's what you want, unfortunately the language does not support that feature. C++ requires you be a bit more verbose with those sorts of things at present time. If you need more than what boost::lamda is already providing you then you should probably separate it out as a normal function anyway.


In C and C++ this is accomplished using function pointers or functors and templates (C++ only).

For example (using the C++ way (functors))

//Define a functor. A functor is nothing but a class which overloads
//operator(). Inheriting from std::binary_function allows your functor
//to operate cleanly with STL algorithms.
struct MyFunctor : public std::binary_function<int, int, bool>
{
    bool operator()(int a, int b) {
        return a < b;
    };
};

//Define a template which takes a functor type. Your functor should be
//should be passed by value into the target function, and a functor should
//not have internal state, making this copy cheap.
template <typename Func_T>
void MyFunctionUsingACallback(Func_T functor)
{
    if (functor(a, b))
        //Do something
    else
        //Do something else
}

//Example usage.
int main()
{
    MyFunctionUsingACallback(MyFunctor());
}

Using the C way (function pointers):

//Create a typedef for a function pointer type taking a pair of ints and
//returning a boolean value.
typedef bool (*Functor_T)(int, int);

//An example callback function.
bool MyFunctor(int a, int b)
{
    return a < b;
}

//Note that you use the typedef'd function here.
void MyFunctionUsingACallback(Functor_T functor)
{
    if (functor(a, b))
        //Do something
    else
        //Do something else
}

//Example usage.
int main()
{
    MyFunctionUsingACallback(MyFunctor);
}

Note that you should prefer the C++ way because it will allow the compiler to make more intelligent decisions with regards to inlining, unless for some reason you are limited to the C subset.

Billy ONeal
+1  A: 

If your compiler is a fairly recent release (such as Visual Studio 2010 or GCC 4.5), you can use some new features from the new C++ standard, which is currently in ratification and should be published soon.

I don't know what you need to do to enable this in Visual Studio, but it should be well-documented either on MSDN or internal help.

For GCC 4.5, just add the -std=c++0x option to enable the new features.

One of these features is the Lambda syntax:

template <typename F>
void func_with_callback(F f) {
    f(true);
}

int main() {
    func_with_callback( [](bool t){ if(t) cout << "lambda called" << endl; } );
}

If you don't have access to a modern compiler, you can use techniques such as functors and libraries like boost::lambda, which can perform similarly.

greyfade
This is exactly what I was looking for. I already fiddled around with templates and lambda expressions, but couldn't get it working. On a side note, VS2010 let's you use this with no additional changes.
Matthias Vance