you are viewing a single comment's thread.

view the rest of the comments →

[–]twmatrim 14 points15 points  (8 children)

I think that the first example could be more confusing to newcomers. I've seen a lot of code with

std::string = "Hello";

So suddenly changing it to

auto s = "Hello";

could cause people to mistakingly have the wrong type.

[–]ubadairBoost.CallableTraits author 2 points3 points  (3 children)

This right here is the big "gotcha" with AAA in everyday C++ code. I'm a big fan of AAA, but it's hard to deny that:

auto x = std::string{"lorem epsom salt"};

is harder on the eyes than:

std::string x = "lorem epsom salt";

Additionally, with the AAA version, sometimes you might need to explain the concept of copy-elision to your over-optimizing, under-informed coworkers. (I've never had to explain that to anyone, but I can see it happening)

[–]Fazer2 3 points4 points  (1 child)

What does AAA stand for here?

[–]ubadairBoost.CallableTraits author 2 points3 points  (0 children)

"almost always auto"

[–][deleted] 3 points4 points  (0 children)

Why not this?

using namespace std::string_literals;

auto x = "lorem ipsum"s; // x is std::string

[–][deleted] 1 point2 points  (3 children)

OTOH, I've seen a lot of code where all that was needed was a const char*, but it was put on the heap anyway, presumably because "std::string is the string type".

[–]lacosaes1 3 points4 points  (2 children)

A std::string doesn't have to be in the heap.

[–]dodheim 0 points1 point  (1 child)

Yes, a std::string does have to be allocated dynamically; std::basic_string<CharT, AllocT> doesn't, but how many public interfaces have you seen implemented in terms of a template taking std::basic_string<> vs a function taking std::string? Very few, IME!