Reputation: 9527
Here's a program where main
has two lambdas defined. a
calls b
:
#include <iostream>
int main()
{
auto b = []() { std::cout << "b" << std::endl; };
auto a = [&]() { b(); };
a();
}
Now, since b
is only used by a
, it can also be defined inside a
:
#include <iostream>
int main()
{
auto a = []()
{
auto b = []() { std::cout << "b" << std::endl; };
b();
};
a();
}
My question is this: Let's suppose a
is called repeatedly in a loop and so there are efficiency concerns. Can most compilers be expected to make the b-inside-a version as efficient as the b-outside-a version?
Upvotes: 0
Views: 63
Reputation: 385295
It's better than that; both programs will be identical to:
#include <iostream>
int main()
{
std::cout << "b" << std::endl;
}
Upvotes: 1