Can I define functions in C++ inline? I am talking about lambda functions, not the inline
keyword that causes a compiler optimization.

- 11,022
- 1
- 32
- 59

- 32,406
- 45
- 166
- 297
-
1I believe in the new C++11 syntax, lambdas might provide something similar to what you wish to achieve? – im so confused Sep 18 '12 at 19:35
-
1Yeah, in C++11, to pass one that adds two ints it'd be `[](int num1, int num2) {return num1 + num2);}`. – chris Sep 18 '12 at 19:37
-
1possible duplicate of [What is a lambda expression in C++11?](http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) – Mooing Duck Sep 18 '12 at 19:43
-
what if I need to: `int n = 1==2?function(){return 10;} : -1;` something like that? – vincent thorpe Jul 03 '19 at 10:52
4 Answers
C++11 added lambda functions to the language. The previous versions of the language (C++98 and C++03), as well as all current versions of the C language (C89, C99, and C11) do not support this feature. The syntax looks like:
[capture](parameters)->return-type{body}
For example, to compute the sum of all of the elements in a vector:
std::vector<int> some_list;
int total = 0;
for (int i=0;i<5;i++) some_list.push_back(i);
std::for_each(begin(some_list), end(some_list), [&total](int x) {
total += x;
});

- 119
- 2
- 7

- 390,455
- 97
- 512
- 589
-
9
-
1Any particular reason this was downvoted? It answers the question just fine. – chris Sep 18 '12 at 19:44
-
Isn't the code missing a `mutable`? IIRC lambdas take captures as `const` by default, so it should be `[&total](int x) mutable`. – Grizzly Sep 18 '12 at 19:47
-
1@Grizzly: That's only interesting for by-value captures, since `const`ness doesn't propagate through indirection. – Xeo Sep 18 '12 at 19:48
-
Where is `begin(...)` and `end(...)` are defined? Are they macros for vector.begin() and vector.end() ? – Sep 18 '12 at 19:55
-
1@Desolator : They're in namespace `std`, found by ADL. And yes, they wrap `some_list.begin()` and `some_list.end()` in this case. – ildjarn Sep 18 '12 at 20:10
-
In C++11, you can use closures:
void foo()
{
auto f = [](int a, int b) -> int { return a + b; };
auto n = f(1, 2);
}
Prior to that, you can use local classes:
void bar()
{
struct LocalClass
{
int operator()(int a, int b) const { return a + b; }
} f;
int n = f(1, 2);
}
Both versions can be made to refer to ambient variables: In the local class, you can add a reference member and bind it in the constructor; and for the closure you can add a capture list to the lambda expression.

- 464,522
- 92
- 875
- 1,084
-
-
@Xeo: That's true, but I didn't think that was a requirement... the OP is welcome to clarify. – Kerrek SB Sep 18 '12 at 19:52
-
Oh, I didn't mean it that way. It was a general reminder, since you didn't mention that restriction. – Xeo Sep 18 '12 at 19:53
-
3Neither of your examples form closures. The first example does form an anonymous function though. – Thomas Eding Dec 07 '16 at 06:44
-
@ThomasEding: That depends on your personal disposition towards the empty set, I suppose. – Kerrek SB Dec 07 '16 at 13:09
i dont know if i understand you well, but you want a lambda function?
http://en.cppreference.com/w/cpp/language/lambda
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
int main()
{
std::vector<int> c { 1,2,3,4,5,6,7 };
int x = 5;
c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; } ), c.end());
std::cout << "c: ";
for (auto i: c) {
std::cout << i << ' ';
}
std::cout << '\n';
std::function<int (int)> func = [](int i) { return i+4; };
std::cout << "func: " << func(6) << '\n';
}
if you dont have c++11x then try:

- 2,783
- 1
- 21
- 31
Pre C++11, if you want to localize a function to a function, that can be done:
int foo () {
struct Local {
static int bar () {
return 1;
}
};
return Local::bar();
}
or if you want something more complicated:
int foo (int x) {
struct Local {
int & x;
Local (int & x) : x(x) {}
int bar (int y) {
return x * x + y;
}
};
return Local(x).bar(44);
}
But if you want a true function literal in pre C++11, that is not possible.

- 35,312
- 13
- 75
- 106