Quantcast
Channel: How can I write a beautiful inline recursive lambda in C++? - Stack Overflow
Browsing all 6 articles
Browse latest View live

Answer by Artyer for How to write a beautiful inline recursive lambda in C++?

You can use an explicit object parameter with a deduced type:auto dfs = [&](this const auto &self, int x) -> void { // .... self(x);};dfs(0); // `self` is `dfs`This is a C++23 feature

View Article


How to write a beautiful inline recursive lambda in C++?

As I know in C++ 17, I can write a recursive lambda like this:auto dfs = [&](const auto &self, int x) -> void { // .... self(self, x);};dfs(dfs, 0);Unfortunately, I have to endure one more...

View Article

Answer by sudo rm -rf slash for How can I write a beautiful inline recursive...

I'm not sure if this counts as beautiful, but you might consider this:std::function<void(int)> dfs = [&](int x) -> void { // .... dfs(x);};dfs(0);The compiler may be able to inline the...

View Article

Answer by Artyer for How can I write a beautiful inline recursive lambda in C++?

You can use an explicit object parameter with a deduced type:auto dfs = [&](this const auto &self, int x) -> void { // .... self(x);};dfs(0); // `self` is `dfs`This is a C++23 feature

View Article

How can I write a beautiful inline recursive lambda in C++?

As I know in C++17, I can write a recursive lambda like this:auto dfs = [&](const auto &self, int x) -> void { // .... self(self, x);};dfs(dfs, 0);Unfortunately, I have to endure one more...

View Article


Answer by Neil for How can I write a beautiful inline recursive lambda in C++?

If you don't have access to C++23 then you can wrap your lambda in another lambda that hides the extra argument, e.g. like this:int main(int argc, char **argv) { auto fact = [&](int x) -> int {...

View Article
Browsing all 6 articles
Browse latest View live