Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comment on ep205 lambda as a way to take benefit of (N)RVO #443

Open
Dharmesh946 opened this issue Nov 18, 2024 · 1 comment
Open

comment on ep205 lambda as a way to take benefit of (N)RVO #443

Dharmesh946 opened this issue Nov 18, 2024 · 1 comment

Comments

@Dharmesh946
Copy link

Sorry I didn't find the ep205 corresponding "issue".

In this episode and in some previous one on lambda. It is claimed that lambdas can be used to optimize some variable creation.
The canonical example given is

#include <vector>

void f() {
    const auto value = []() {
        std::vector<int> vec(10, 10);
        vec.push_back(42);
        return vec;
    }();
}

Yet

#include <vector>

void g() {
    std::vector<int> vec(10, 10);
    vec.push_back(42);
    const auto value = std::move(vec);
}

seems to lead to the exact same assembly : https://godbolt.org/z/bc6TxY7vW.
This more verbose snippet, shows also that NRVO might fail inside the lambda (which are submitted to the same rules as regular functions) : https://godbolt.org/z/hG1qT18K1

Would it be possible to have a clarification?

@Dharmesh946
Copy link
Author

from https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-lambda-init I guess that it comes useful in case of not movable intermediate object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant