Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to shorten the C++11 lambda signature in declaration?

Tags:

I want to shorten the following type of lambdas:

[] (SomeVeryLongTemplateType<int, float, char, std::string>, AnotherLongType) {}; 

Since the only reason for this lambda is to initialize some class std::function<...> member - it doesn't capture anything, it doesn't have argument names, it returns nothing, it does nothing.

If the shortening operation is expressed as a function of the number of arguments in signature, then I want this function to have the complexity O(1).

Is there a way to do that?

like image 663
abyss.7 Avatar asked Feb 04 '14 06:02

abyss.7


2 Answers

Looks like you're looking for an empty lambda which does nothing, so that your std::function object will always be in callable state!

If so, then use this one which can be reused, for any number of parameters:

static const struct empty_lambda_t //static and const applies to the object! {       template<typename ...T>       void operator()(T && ... ) const {} //does nothing  }empty_lambda {}; //declare an object which is static and const 

And then use it as:

 std::function<void()>          fun1 = empty_lambda;  std::function<void(int,int)>   fun2 = empty_lambda;  std::function<void(whatever)>  fun3 = empty_lambda; 

Hope that helps.

like image 74
Nawaz Avatar answered Oct 01 '22 06:10

Nawaz


in C++14 there will be "generic lambdas" that should simplify long type names in parameters as I understand:

auto lambda = [](auto x, auto y) {return x + y; }; 

here auto is like Template type

http://en.wikipedia.org/wiki/C%2B%2B14#Generic_lambdas

like image 34
fen Avatar answered Oct 01 '22 05:10

fen