News for hackers. Stuff that matters.
Re:Formatting Text in C++: The Old and the New Ways by jcelerier on Saturday September 16, 2023 @04:59PM (#37538636)

    for (auto [s, n]: my_map)
copies all the data needlessly, better to use

    for (const auto& [s, n]: my_map)
Re:Formatting Text in C++: The Old and the New Ways by nuancebydefault on Saturday September 16, 2023 @05:34PM (#37538931)
That's why some newer languages have 'const are the default'.
Re:Formatting Text in C++: The Old and the New Ways by Tommstein on Saturday September 16, 2023 @05:40PM (#37538980)
I'm pretty sure it's the reference that makes the data not be copied.
Re:Formatting Text in C++: The Old and the New Ways by nuancebydefault on Saturday September 16, 2023 @06:12PM (#37539291)
Copying the data to a const makes little sense in this case. The extra & choice that has emerged makes things more complicated than needed. The sad faith of this old language.
Re:Formatting Text in C++: The Old and the New Ways by nly on Saturday September 16, 2023 @06:49PM (#37539584)
Without the const the key would still be const.
Re:Formatting Text in C++: The Old and the New Ways by heywhatupboys on Saturday September 16, 2023 @05:11PM (#37538748)
what a language
Re:Formatting Text in C++: The Old and the New Ways by lionkor on Saturday September 16, 2023 @05:37PM (#37538962)
just consider const reference to be the default for non-primitive types
Re:Formatting Text in C++: The Old and the New Ways by a_e_k on Saturday September 16, 2023 @06:47PM (#37539566)
My rule of thumb is to use const references when the sizeof the type is larger than the sizeof a pointer or reference.
Re:Formatting Text in C++: The Old and the New Ways by repsilat on Saturday September 16, 2023 @10:06PM (#37540776)
Might be worth using values/copies even a bit bigger, so long as it's "simple" data. This[1] short post argues for passing `std::string_view` (~2 pointers) by value, for

- Omitting pointer indirections (loads),

- Not forcing the pointee to have an address (i.e. gotta be in memory, not just registers), and

- Eliminating aliasing questions, potentially leading to better codegen if the function isn't inlined.

1: https://quuxplusone.github.io/blog/2021/11/09/pass-string-vi...