Skip to main content
The 2024 Developer Survey results are live! See the results

All Questions

Tagged with
1 vote
1 answer
70 views

How to std::accumulate chars in a string as digits using lambda?

How can I sum all digits in a string num into an integer sum using a lambda? std::string num = "1234567891011"; int sum = accumulate(num.begin(), num.end(), 0, [](auto & a, auto & b)...
javierMarquez's user avatar
-4 votes
1 answer
28 views

Why can't I call this lambda function?

Below is a simplified version of the section of code that's causing the problem: void reorient(C new_pos) { // Orientate north if (pos + 0x10 == new_pos) { if ((orient == 0) && (Gz &...
user17301834's user avatar
2 votes
4 answers
192 views

C++. Accumulate binary array to integer by using custom lambda

How can I accumulate a binary array containing 0 and 1 into an integer? vector<int> arr = {1,0,1,0,1,1,1,0,1,0,0}; int num = accumulate( arr.begin(), arr.end(), [](int &a, int &...
javierMarquez's user avatar
1 vote
2 answers
652 views

Fix "lambda has no capture-default" [duplicate]

I'm using lambdas for the first time and having a problem which I can't figure out. I'm getting multiple errors: <source>: In lambda function: <source>:7:19: error: 'x' is not captured ...
BBadger's user avatar
  • 37
6 votes
2 answers
123 views

Is it legal to call the pointer which points to a lambda that is out of scope?

Is it legal to call the pointer which points to a lambda that does not exist anymore? Here is the demo code snippet. #include <iostream> typedef int (*Func)(int a); int main() { Func fun; { ...
John's user avatar
  • 3,354
0 votes
0 answers
41 views

Should I use lambda functions where a free function could have been used? [duplicate]

Imagine I have a .cpp file, which inside has some helper code. I can put that code inside some anonymous namespace. Are there disadvantages to use a lambda function for this? Example: namespace { ...
Maxim Chetrusca's user avatar
1 vote
2 answers
221 views

Where is a lambda stored?

This is follow-up question to Does a C++ lambda have a limited life?. In my previous question, trying to use a lambda after it had gone out of scope caused a crash. While it shouldn't have been ...
Rocketmagnet's user avatar
  • 5,829
43 votes
4 answers
7k views

Lambda passed by reference runs when invoked in the constructor, but not when later stored in a data member

The following C++ code prints 11.1 then crashes. The lambda function seems to be called correctly inside the constructor, but then later, that same function no longer works! Why is this happening? ...
Rocketmagnet's user avatar
  • 5,829
3 votes
2 answers
76 views

Why copy constructor is called in capture list of lambda

#include <iostream> #include <vector> #include <functional> using namespace std; struct B { B() { std::cout<<"B"<<std::endl; } B(B&...
MengMeng's user avatar
  • 1,024
0 votes
1 answer
90 views

why should I use function instead of named lambdas in C++

Recently I've struggled a lot with function deduction and of course lambdas prove to be more flexible here, but doing so I've came to a question, why should I use function instead of named lambdas? e....
Mahashi's user avatar
  • 119
3 votes
1 answer
84 views

Returning with a Local Variable in a Function Template

In the example below the function template is returning with a local variable and it works as expected even though the return value is not a reference. Is there a lifetime extension scenario in here? &...
terto's user avatar
  • 107
0 votes
1 answer
127 views

Can function templates slow compilation

I have a class with a function template. class Foo { public: void Foo_fn1(); template <typename Closure> void Foo_util (Closure&& closure) { Foo_fn1(); std::forward<...
Aman Deep Gautam's user avatar
2 votes
1 answer
169 views

Function composition with functor - Performance overhead

As we know, in C++ 11, can't deduce the return type of function returning lambda with auto or decltype(auto). So can't use code like below. template <typename F, typename G> constexpr decltype(...
lighthouse's user avatar
1 vote
2 answers
91 views

function::target(): How does the template type work for lambdas?

I want to store a lambda expression in a function object, then retrieve its underlying function pointer. For reasons I do not understand, function::target() is a templated function that returns ...
Daniel Handojo's user avatar
27 votes
2 answers
2k views

Can Different Lambdas Decay to the Same Function Pointer?

Can two different lambdas (that have no capture, and the same args and body) decay to the same function pointer? I have this code: #include <cassert> #include <type_traits> int main() { ...
Ben's user avatar
  • 9,587

15 30 50 per page
1
2 3 4 5
128