The type of a lambda expression is unspecified.

But they are generally mere syntactic sugar for functors. A lambda is translated directly into a functor. Anything inside the [] are turned into constructor parameters and members of the functor object, and the parameters inside () are turned into parameters for the functor's operator().

A lambda which captures no variables (nothing inside the []'s) can be converted into a function pointer (MSVC2010 doesn't support this, if that's your compiler, but this conversion is part of the standard).

But the actual type of the lambda isn't a function pointer. It's some unspecified functor type.

Answer from Stack Overflow is garbage on Stack Overflow
🌐
Standard C++
isocpp.org › wiki › faq › cpp14-language
C++14 Language Extensions, C++ FAQ
Lambda function parameters can now be auto to let the compiler deduce the type.
Discussions

Why does C not have lambdas/anonymous function expressions?
There are two basic ways to implement closures: A custom calling convention: Callers are aware they're calling a closure and so pass the closure context to the callee, perhaps as a hidden argument. Closures are nearly always implemented this way, including in C++. For C, as a lingua franca platforms would need to define this calling convention / ABI so that different implementers could all each others closures just as they can call each others functions. Since this doesn't exist, closures in language implementations using this approach are incompatible with C interop (ex. can't turn a C++ closure into a function pointer). Build a trampoline by allocating a little bit of executable memory, essentially like using a small JIT compiler. Callers do not need to be aware they're calling a closure, and a plain C calling convention is sufficient. GNU C closures are implemented this way, as are CPython's ctypes callbacks. However, frequently allocating executable memory requires significant trade-offs in performance or security. It may not even be possible on some platforms. This is also an implicit allocation — which is not in the spirit of C — and someone has to manage its lifetime. (GNU C manages it by using an automatic allocation.) Unless you're willing to make the trade-offs in the second approach — which is available to you if you use GCC — neither fits C well. More on reddit.com
🌐 r/C_Programming
35
6
August 7, 2022
what's the type of a lambda expression ? - C++ Forum
Now, I am turning my code into ... of the type of some of the objects in the .h file; even with the help of decltype(), it is challenging because the expressions needed as an argument are rather convoluted. Most of the difficulty comes from the use of lambda expres... More on cplusplus.com
🌐 cplusplus.com
February 18, 2019
c++ - what is the type signature of a c++11/1y lambda function? - Stack Overflow
I was wondering if there is a standard way to get the type signature (i.e. the return type and the types) of its parameters of any given lambda? The reason I ask is that I've always wondered what More on stackoverflow.com
🌐 stackoverflow.com
simple lambda like functions in C
These nested functions aren’t standard C and they won’t work on Clang or MSVC. More on reddit.com
🌐 r/C_Programming
26
11
March 12, 2024
🌐
Cprogramming.com
cprogramming.com › c++11 › c++11-lambda-closures.html
C++11 - Lambda Closures, the Definitive Guide - Cprogramming.com
How to begin Get the book · C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials
🌐
cppreference.com
en.cppreference.com › cpp › language › lambda
Lambda expressions (since C++11) - cppreference.com
The lambda expression is a prvalue expression of unique unnamed non-union non-aggregate class type, known as closure type, which is declared (for the purposes of ADL) in the smallest block scope, class scope, or namespace scope that contains the lambda expression.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › lambda-expression-in-c
Lambda Expression in C++ - GeeksforGeeks
#include <iostream> using namespace std; int main() { // Defining a lambda auto res = [](int x) { return x + x; }; // Using the lambda cout << res(5); return 0; } ... Explanation: The lambda expression in the above program takes an integer x as input and returns the sum of x with itself. ... Parameters: These parameters are similar to the function parameters in every way. Return Type: Generally, the return-type in lambda expressions is evaluated by the compiler itself and we don’t need to specify it explicitly.
Published   April 26, 2026
🌐
Learn C++
learncpp.com › cpp-tutorial › introduction-to-lambdas-anonymous-functions
20.6 — Introduction to lambdas (anonymous functions) – Learn C++
January 3, 2020 - Storing a lambda in a variable provides a way for us to give the lambda a useful name, which can help make our code more readable. Storing a lambda in a variable also provides us with a way to use that lambda more than once. ... As it turns out, lambdas don’t have a type that we can explicitly use.
🌐
Reddit
reddit.com › r/c_programming › why does c not have lambdas/anonymous function expressions?
r/C_Programming on Reddit: Why does C not have lambdas/anonymous function expressions?
August 7, 2022 -

It would not seem to hard to implement to allow a programmer to use a construct similar to:

int (*add)(int, int) = (int(int x, int y)){return x+y;};

This would simplify code that requires callback functions such as qsort or bsearch or various UI libraries that use callbacks to define, for example, a buttons behavior when pressed. Is there any specific reason they elected not to support this, and require us to define named static functions instead?

Top answer
1 of 7
8
There are two basic ways to implement closures: A custom calling convention: Callers are aware they're calling a closure and so pass the closure context to the callee, perhaps as a hidden argument. Closures are nearly always implemented this way, including in C++. For C, as a lingua franca platforms would need to define this calling convention / ABI so that different implementers could all each others closures just as they can call each others functions. Since this doesn't exist, closures in language implementations using this approach are incompatible with C interop (ex. can't turn a C++ closure into a function pointer). Build a trampoline by allocating a little bit of executable memory, essentially like using a small JIT compiler. Callers do not need to be aware they're calling a closure, and a plain C calling convention is sufficient. GNU C closures are implemented this way, as are CPython's ctypes callbacks. However, frequently allocating executable memory requires significant trade-offs in performance or security. It may not even be possible on some platforms. This is also an implicit allocation — which is not in the spirit of C — and someone has to manage its lifetime. (GNU C manages it by using an automatic allocation.) Unless you're willing to make the trade-offs in the second approach — which is available to you if you use GCC — neither fits C well.
2 of 7
6
Why are people obsessed with making C like python or whatever language they learned first? It's like asking why my bicycle only has two wheels, when your red wagon has four.
Find elsewhere
🌐
Embarcadero
blogs.embarcadero.com › the-advanced-guide-to-lambda-expression-in-c-software
The Advanced Guide To Lambda Expression In C++ Software – Embarcadero RAD Studio, Delphi, & C++Builder Blogs
September 25, 2022 - The syntax for a lambda expression consists of specific punctuation with = [ ] ( ) { ... } series. ... Datatype is its type like int, float, class, etc.
🌐
Hackaday
hackaday.com › 2019 › 09 › 11 › lambdas-for-c-sort-of
Lambdas For C — Sort Of | Hackaday
November 2, 2023 - But in general ‘lambdas’ imply closures, which is a much more powerful concept: a closure also has access to its defining scope, which means that it lets you abstract over the data required to make a particular implementation of a function work. This is kind-of equivalent to passing a ‘data’ argument along with the function pointer, as is common in C, but has an important distinction: the *type...
🌐
Medium
medium.com › @briankworld › introduction-to-c-lambdas-and-using-them-with-standard-library-algorithms-bef29ef80dd8
Introduction to C++ Lambdas and Using Them with Standard Library Algorithms | by Brian | Medium
May 6, 2023 - The basic syntax of lambda expression is: [capture-list](parameter-list) -> return-type { function-body } where: capture-list: specifies the variables captured from the enclosing scope · parameter-list: specifies the parameters of the lambda function · return-type: specifies the return type of the lambda function ·
🌐
Cplusplus
cplusplus.com › forum › general › 250013
what's the type of a lambda expression ? - C++ Forum
February 18, 2019 - Don't let the name that you see in the error message fool you. Even if two lambdas appear to have the same name they are still different types. If you need the lambdas to have a common type you can wrap them up into std::functions, which can even be used on functions and other callable things, not just lambdas.
Top answer
1 of 4
20

You are correct the types of C++11 lambdas are anonymous and instance-unique. the std::function type can store references to any kind of lambda I have come across, but there is said to be a performance hit.

Try

std::function<int (int, int)> f =  -> int { 
    return x + y; 
};

note the -> int can be omitted in non ambiguous scenarios such as this.

C++14 lets us write

std::function<int (int, int)> f =  { 
    return x + y; 
};

which is handy for long type names.

As noted by @Jonathan Wakely, this approach captures a specific instantiation using std::function with fixed template arguments. In C++14, template variables can be specified. Additionally, also per C++14, lambda parameters can have can have their types inferred via auto, allowing for the following:

template<class T>
std::function<T (T, T)> g =  -> auto {
    return x + y;
};

Currently, VC++, and GCC do not seem to support templates on variable declarations at function level, but allow them on member, namespace, and global declarations. I am unsure whether or not this restriction emanates from the spec.

Note: I do not use clang.

2 of 4
18

According to Can the 'type' of a lambda expression be expressed?, there is actually a simple way in current c++ (without needing c++1y) to figure out the return_type and parameter types of a lambda. Adapting this, it is not difficult to assemble a std::function typed signature type (called f_type below) for each lambda.

I. With this abstract type, it is actually possible to have an alternative way to auto for expressing the type signature of a lambda, namely function_traits<..>::f_type below. Note: the f_type is not the real type of a lambda, but rather a summary of a lambda's type signature in functional terms. It is however, probably more useful than the real type of a lambda because every single lambda is its own type.

As shown in the code below, just like one can use vector<int>::iterator_type i = v.begin(), one can also do function_traits<lambda>::f_type f = lambda, which is an alternative to the mysterious auto. Of course, this similarity is only formal. The code below involves converting the lambda to a std::function with the cost of type erasure on construction of std::function object and a small cost for making indirect call through the std::function object. But these implementation issues for using std::function aside (which I don't believe are fundamental and should stand forever), it is possible, after all, to explicitly express the (abstract) type signature of any given lambda.

II. It is also possible to write a make_function wrapper (pretty much like std::make_pair and std::make_tuple) to automatically convert a lambda f ( and other callables like function pointers/functors) to std::function, with the same type-deduction capabilities.

Test code is below:

#include <cstdlib>
#include <tuple>
#include <functional>
#include <iostream>
using namespace std;

// For generic types that are functors, delegate to its 'operator()'
template <typename T>
struct function_traits
    : public function_traits<decltype(&T::operator())>
{};

// for pointers to member function
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
    //enum { arity = sizeof...(Args) };
    typedef function<ReturnType (Args...)> f_type;
};

// for pointers to member function
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) > {
    typedef function<ReturnType (Args...)> f_type;
};

// for function pointers
template <typename ReturnType, typename... Args>
struct function_traits<ReturnType (*)(Args...)>  {
  typedef function<ReturnType (Args...)> f_type;
};

template <typename L> 
typename function_traits<L>::f_type make_function(L l){
  return (typename function_traits<L>::f_type)(l);
}

long times10(int i) { return long(i*10); }

struct X {
  double operator () (float f, double d) { return d*f; } 
};

// test code
int main()
{
    auto lambda =  { return long(i*10); };
    typedef function_traits<decltype(lambda)> traits;
    traits::f_type ff = lambda;

    cout << make_function( { return long(i*10); })(2) << ", " << make_function(times10)(2) << ", " << ff(2) << endl;
    cout << make_function(X{})(2,3.0) << endl;

    return 0;
}
🌐
Sticky Bits
blog.feabhas.com › home › demystifying c++ lambdas
Demystifying C++ lambdas - Sticky Bits - Powered by FeabhasSticky Bits – Powered by Feabhas
September 3, 2014 - Creating bespoke functors can be a lot of effort; especially if the functor is only used in one specific place. These bespoke functors also unnecessarily ‘clutter up’ the code. A lambda is an ad-hoc, locally-scoped function (well, more strictly, ...
🌐
W3Schools
w3schools.com › cpp › cpp_functions_lambda.asp
C++ Lambda Functions
C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Code Challenges C++ Practice Problems C++ Syllabus C++ Study Plan ... A lambda function is a small, anonymous function you can write directly in your code.
🌐
LinkedIn
linkedin.com › pulse › lamda-unction-c-amit-nadiger
Lamda function in C++
June 18, 2023 - Lambda functions in C++ are treated as special anonymous function objects by the compiler. When you define a lambda function, the compiler generates a unique closure type for that lambda, which encapsulates the lambda's captured variables (if ...
🌐
Quora
quora.com › How-do-we-write-and-use-lambda-functions-in-pure-C-indirectly-instead-of-C
How do we write and use lambda functions in pure C indirectly instead of C++? - Quora
Answer (1 of 5): https://en.wikipedia.org/wiki/Anonymous_function#C_(non-standard_extension) > C (non-standard extension)[edit] The anonymous function is not supported by standard C programming language, but supported by some C dialects, such as GCC[52] and Clang. GCC[edit] GNU Compiler Colle...
🌐
DEV Community
dev.to › sandordargo › lambda-expressions-in-c-4pj4
Lambda Expressions in C++ - DEV Community
September 4, 2020 - Yes, it's that easy. Are you interested in its type? Try using decltype to get it. Let's move on. Something that is really nice about C++ lambdas is that you can practice English. You have all types of brackets in it. You will have to deal with parentheses or round brackets (()), square or ...
🌐
DZone
dzone.com › articles › all-about-lambda-functions-in-cfrom-c11-to-c17
All About Lambda Functions in C++ (From C++11 to C++17)
May 8, 2020 - In other words, it's just syntactic sugar. lambda function syntax is defined as: ... Usually, the compiler evaluates a return type of a lambda function itself. So we don't need to specify a trailing return type explicitly i.e.