Can lambda functions be templated?

In C++11, is there a way to template a lambda function? Or is it inherently too specific to be templated?

I understand that I can define a classic templated class/functor instead, but the question is more like: does the language allow templating lambda functions?


  • UPDATE 2018: C++20 will come with templated and conceptualized lambdas. The feature has already been integrated into the standard draft.

    UPDATE 2014: C++14 has been released this year and now provides Polymorphic lambdas with the same syntax as in this example. Some major compilers already implement it.

    At it stands (in C++11), sadly no. Polymorphic lambdas would be excellent in terms of flexibility and power.

    The original reason they ended up being monomorphic was because of concepts. Concepts made this code situation difficult:

    template <Constraint T>
    void foo(T x)
        auto bar = [](auto x){}; // imaginary syntax

    In a constrained template you can only call other constrained templates. (Otherwise the constraints couldn't be checked.) Can foo invoke bar(x)? What constraints does the lambda have (the parameter for it is just a template, after all)?

    Concepts weren't ready to tackle this sort of thing; it'd require more stuff like late_check (where the concept wasn't checked until invoked) and stuff. Simpler was just to drop it all and stick to monomorphic lambdas.

    However, with the removal of concepts from C++0x, polymorphic lambdas become a simple proposition again. However, I can't find any proposals for it. :(