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

Output parameters vs return values #129

Open
dimztimz opened this issue Oct 18, 2024 · 1 comment
Open

Output parameters vs return values #129

dimztimz opened this issue Oct 18, 2024 · 1 comment

Comments

@dimztimz
Copy link

My comments are mostly for the C++ proposal P1729. In the first revision the API used output parameters, similar to scanf and iostreams, but in the second revision it was changed to use return value. Now, the newer API might be more ergonomic (arguable), but it can be less performant, especially when reading strings.

Consider the following example:

std::string key;
int value;
for (...) {
    std::scan(some_input, "{} = {}", key, value);
}

versus

for (...) {
    auto result = std::scan<std::string, int>(some_input, "{} = {}")
}

The first example is more performant because it will reuse the same std::string object and it will avoid unnecesarry allocations. In the second example the std::string object will be constructed and destructed in each loop iteration, potentially allocating in each iteration. SSO saves something, but in the first example we save much more unnecessary allocations.

@TD22057
Copy link

TD22057 commented Nov 10, 2024

I agree w/ this comment. IMO the v1 API was simpler to use, easier to understand, and easier to see how the format string tied to values. Converting our code to the new version has resulting in much longer and more confusing parse statements.

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

2 participants