Lambda functions as base classes

Playing with Lambdas, I found an interesting behavior that I do not quite understand.

Suppose I have a struct Overload that is inferred from two template parameters and has a suggestion using F1::operator(); .

Now, if I get two functors, I can only access operator () F1 (as you would expect)

If I get two Lambda functions, this is no longer the case: I can also access the operator () from F2.

 #include <iostream> // I compiled with g++ (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8) // // g++ -Wall -std=c++11 -g main.cc // g++ -Wall -std=c++11 -DFUNCTOR -g main.cc // // or clang clang version 3.3 (tags/RELEASE_33/rc2) // // clang++ -Wall -std=c++11 -g main.cc // clang++ -Wall -std=c++11 -DFUNCTOR -g main.cc // // on a Linux localhost.localdomain 3.9.6-200.fc18.i686 #1 SMP Thu Jun 13 // 19:29:40 UTC 2013 i686 i686 i386 GNU/Linux box struct Functor1 { void operator()() { std::cout << "Functor1::operator()()\n"; } }; struct Functor2 { void operator()(int) { std::cout << "Functor2::operator()(int)\n"; } }; template <typename F1, typename F2> struct Overload : public F1, public F2 { Overload() : F1() , F2() {} Overload(F1 x1, F2 x2) : F1(x1) , F2(x2) {} using F1::operator(); }; template <typename F1, typename F2> auto get(F1 x1, F2 x2) -> Overload<F1, F2> { return Overload<F1, F2>(x1, x2); } int main(int argc, char *argv[]) { auto f = get(Functor1(), Functor2()); f(); #ifdef FUNCTOR f(2); // this one doesn't work IMHO correctly #endif auto f1 = get( []() { std::cout << "lambda1::operator()()\n"; }, [](int) { std::cout << "lambda2::operator()(int)\n"; } ); f1(); f1(2); // this one works but I don't know why return 0; } 

The standard states that:

The type of lambda expression (which is also the type of a trailing object) is a unique, unnamed type of non-unit class

Therefore, each type of lambda must be unique.

I can’t explain why this is so: can anyone shed some light on this, please?

+66
c ++ lambda c ++ 11
Aug 25 '13 at 18:27
source share
2 answers

In addition to operator() , a class defined by a lambda can (under the right circumstances) provide a conversion to a pointer to a function. The circumstance (or at least primary) is that lambda cannot capture anything.

If you add capture:

 auto f1 = get( []() { std::cout << "lambda1::operator()()\n"; }, [i](int) { std::cout << "lambda2::operator()(int)\n"; } ); f1(); f1(2); 

... conversion to pointer to function no longer provided, so trying to compile the code above gives an error, which you probably expected all the time:

 trash9.cpp: In function 'int main(int, char**)': trash9.cpp:49:9: error: no match for call to '(Overload<main(int, char**)::<lambda()>, main(int, char**)::<lambda(int)> >) (int)' trash9.cpp:14:8: note: candidate is: trash9.cpp:45:23: note: main(int, char**)::<lambda()> trash9.cpp:45:23: note: candidate expects 0 arguments, 1 provided 
+32
Aug 25 '13 at 19:01
source share

Lambda generates a functor class.

In fact, you can get lambdas and have polymorphic lambdas!

 #include <string> #include <iostream> int main() { auto overload = make_overload( [](int i) { return '[' + std::to_string(i) + ']'; }, [](std::string s) { return '[' + s + ']'; }, [] { return "[void]"; } ); std::cout << overload(42) << "\n"; std::cout << overload("yay for c++11") << "\n"; std::cout << overload() << "\n"; } 

Print

 [42] [yay for c++11] [void] 

How?

 template <typename... Fs> Overload<Fs...> make_overload(Fs&&... fs) { return { std::forward<Fs>(fs)... }; } 

Of course ... it still hides the magic. This is the Overload class, which “magically” comes from all lambdas and provides the corresponding operator() :

 #include <functional> template <typename... Fs> struct Overload; template <typename F> struct Overload<F> { Overload(F&& f) : _f(std::forward<F>(f)) { } template <typename... Args> auto operator()(Args&&... args) const -> decltype(std::declval<F>()(std::forward<Args>(args)...)) { return _f(std::forward<Args>(args)...); } private: F _f; }; template <typename F, typename... Fs> struct Overload<F, Fs...> : Overload<F>, Overload<Fs...> { using Overload<F>::operator(); using Overload<Fs...>::operator(); Overload(F&& f, Fs&&... fs) : Overload<F>(std::forward<F>(f)), Overload<Fs...>(std::forward<Fs>(fs)...) { } }; template <typename... Fs> Overload<Fs...> make_overload(Fs&&... fs) { return { std::forward<Fs>(fs)... }; } 

Watch Live on Coliru

+14
Aug 25 '13 at 19:08
source share



All Articles