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

All Questions

Tagged with
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) { ...
Fedor's user avatar
  • 19.4k
-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 (...
William Chan's user avatar
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) ] ...
John Glen's user avatar
  • 882
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 ...
Muhammad Mubashir Islam's user avatar
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>() { ...
cppBeginner's user avatar
  • 1,116
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 ...
cppBeginner's user avatar
  • 1,116
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 ...
Montaner's user avatar
  • 534
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 ...
Gene's user avatar
  • 432
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&...
cppbest's user avatar
  • 219
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 " &...
einpoklum's user avatar
  • 127k
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: ...
ligazetom's user avatar
  • 145
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 ...
user2961927's user avatar
  • 1,654
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 ...
Fedor's user avatar
  • 19.4k
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 ...
Fedor's user avatar
  • 19.4k
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 ...
Rahn's user avatar
  • 5,305

15 30 50 per page
1
2 3 4 5
306