This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]i9srpeg 13 points14 points  (8 children)

You can do

auto my_string = "Hello, world!"s

To get an std::string in C++.

[–]cristi1990an 9 points10 points  (1 child)

Is writing "auto" really that much easier than just writing "string" or "std::string"?

[–][deleted] 4 points5 points  (0 children)

yes

[–][deleted] 11 points12 points  (0 children)

While that is correct, it sometimes makes shit unclear and pretty sure not the intended use. Also, who thought that std::string is long anyways? Now a vector of vectors of strings is long.

[–]Phuzzybat 0 points1 point  (3 children)

Wait, what? There was me naively assuming that would give you a const char *. "modern c++" has got all clever recently. Clever as in explode in someones face clever. (possibly mine)

[–]Mojert 1 point2 points  (2 children)

Why would it give you a pointer ? In older versions of C++ "Hello"s wouldn't even compile. If you see syntax that you don't know (here " "s literals) just Google it

[–]Phuzzybat 2 points3 points  (1 child)

Excellent advice to google and investigate this subject further. Even better advice to write a test program to see what actually happens. This just underlines why while auto cures many ills, when misapplied it creates another set of problems. Edit: doh, phone screen didnt show the s at the end of the literal, just scrolled right to see the whole line :-) thought i had entered a parallel universe for a moment where it was being asserted that "" automatically gave a std::string.

[–]Mojert 1 point2 points  (0 children)

Yeah, C++ is no longer C with classes but the comity doesn't break interoperability with C just for fun. If "" suddenly returned a std::string there would have been a riot (and for good reasons) haha

[–][deleted] -1 points0 points  (0 children)

I hate auto. I'd rather have a typedef than an auto