All Questions
4,582
questions
5
votes
1
answer
76
views
Inaccurate compile-time computation of Fibonacci sequence in a recursive lambda
Below is a recursive lambda expression that can compute the values of Fibonacci sequence both in runtime and during constant evaluation:
auto fib = [](this auto && f, auto && p) {
...
-2
votes
0
answers
62
views
Why is static lambda constructed more than once in cpp? [duplicate]
Run this code:
#include <iostream>
void Func(size_t i) {
static auto lambda = [&i]() {
std::cout << i << std::endl;
};
lambda();
}
int main() {
for (...
0
votes
0
answers
88
views
Does the C++ standard library have function types that are non-copy-constructible?
I want to pass a bunch of lambdas around without having to make copies.
This is the declaration of one of the lambdas
[
promise = std::move(promise),
namedSeries = std::move(namedSeries)
] ...
0
votes
0
answers
32
views
How can i correctly use lambda function or CC_CALLBACK Macro for getting input from the user in cocos2d-x?
I am developing a game using cocos2d-x, in which i am trying to get input the path for the directory where i can save the game files. i have implemented it using UI Text Field and Menu Item but ...
2
votes
1
answer
120
views
In GCC, inside a lambda, I can get constexpr variable from a non-constexpr template lambda, but not in Visual C++
This code compiles fine in gcc, but fail in Visual C++.
MCVE = https://godbolt.org/z/K7d5PEs65
int main(){
int localVariable=0; //some local variable
auto lamb=[&]<int s>() {
...
1
vote
2
answers
60
views
get constexpr variable from a lambda function is fine , but compile fail (Visual C++) and fine (gcc) when such statement is in a new lambda
This code compiles fine in gcc, but fail in Visual C++.
MCVE = https://wandbox.org/permlink/SqNI85EospSrwm5T
int main() {
auto func_do1Pass2=[&]() {
return 8;
};
constexpr int ...
0
votes
1
answer
115
views
What is the type of a templated lambda?
The following code tries to create a templated lambda and pass it to a function that calls it. However compilation fails because the type of lambda argument is wrong.
#include <functional>
void ...
1
vote
1
answer
91
views
how to convert lambda function to another one taking tuple of parameters
I need to create a wrapper for lambda function that would take a tuple of parameters of the original lambda function and use its implementation. I'm using C++23 compiler. This is the function I came ...
35
votes
6
answers
4k
views
If a lambda is declared inside a default argument, is it different for each call site?
#include <iostream>
int foo(int x = [](){ static int x = 0; return ++x; }()) {
return x;
};
int main() {
std::cout << foo() << foo(); // prints "12", not "11&...
3
votes
0
answers
69
views
How come I can capture an object being constructed in the lambda doing the construction? [duplicate]
Consider the following code:
int x = [&x](){
std::cout << "Inside lambda, x is " << x << '\n';
x = 5;
return 23;
}();
std::cout << "x is " &...
6
votes
1
answer
95
views
Returning const reference from lambda stored as std::function segfaults [duplicate]
Here is the code:
#include <vector>
#include <functional>
class TestClass {
public:
const std::vector<int> &getStuff() const {
return callback();
}
protected:
...
0
votes
1
answer
52
views
Count the number of arguments in generic lambda
I want to create a function that counts the number of arguments in generic lambda functions. SO user "yuri kilochek" has a great solution Count the number of arguments in a lambda but it ...
4
votes
0
answers
160
views
Using a function's argument to produce the type of another function's argument
Since C++20 abbreviated function templates are available for use with placeholder types in parameter list. And one can declare abbreviated function template g with two arguments of same reference type ...
2
votes
1
answer
95
views
Capture bit-fields by reference in a lambda expression
According to cppreference, bit-fields can only be captured by copy: https://en.cppreference.com/w/cpp/language/lambda. Period.
At the same time, one can see certain scenarios, e.g.:
struct U {
int ...
1
vote
1
answer
106
views
How can I std::move a vector into a lambda function? [duplicate]
#include <iostream>
#include <functional>
int main() {
std::vector<int> integers = {0, 1, 2};
std::function<void()> lambda = [integers] {
printf("vector ...