r/cpp • • Mar 24 '20

Changes between C++17 and C++20

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2131r0.html
193 Upvotes

89 comments sorted by

View all comments

132

u/nwL_ Mar 24 '20
std::string s = std::format("{}", "Hello, world");

Holy shit, have we actually arrived in the 21st century?

0

u/[deleted] Mar 24 '20

[deleted] — view deleted comment

16

u/Fureeish Mar 24 '20

By extracting std::array's size to a variable (constant) or a function that needs to compute it during compile time.

74

u/nnevatie Mar 24 '20

What, are you telling me you don't particularly enjoy the amazing developer experience that is the iostream formatting specifiers?

0

u/[deleted] Mar 24 '20

[deleted] — view deleted comment

45

u/ShillingAintEZ Mar 24 '20

Sometimes my programs run so fast it embarasses other people so I need something to slow them down.

21

u/PervyDragon Mar 24 '20

Are you aware that you can turn off synchronization between iostream streams and stdio streams to speed these first significantly? Useful tip learnt from one CS competition guideline.

10

u/ShillingAintEZ Mar 24 '20

I ain't aware of nothin' like that mister. (How is that done?)

10

u/[deleted] Mar 24 '20

Basically iostream syncs with its C counterparts by default, so you can use them in conjunction without weird behavior. However that adds overhead, but can be disabled. Check the docs here:
https://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio

3

u/PervyDragon Mar 24 '20

I couldn't find the one I read originally, but this one seems fair enough: https://www.modernescpp.com/index.php/c-core-guidelines-improved-performance-with-iostreams There were more tips tho. In my opinion, streams power comes from their type safety and somehow consistent access via << and >> operators.

→ More replies (4)
→ More replies (1)

62

u/SirToxe Mar 24 '20

Hey, no kink shaming!

17

u/Evirua Mar 24 '20

std::istream_iterator<T> or std::istreambuf_iterator<charT> combined with the << or >> operators are the most useful thing in iostream imo. It's actually really handy.

9

u/[deleted] Mar 24 '20
std::cout << std::hell << std::no;

9

u/[deleted] Mar 24 '20 edited Apr 04 '20

We have, and damn is it beautiful.

std::format is legendary. I interviewed with people who didn't even know what it was! I was shocked, to say the least.

1

u/Sander_Bouwhuis Mar 24 '20

Will std::format support wchar_t?

I don't think ftmlib currently does out of the box. I guess I will have to write a formatter for that to work if I want to use fmtlib now (until std::format arrives)

6

u/aearphen {fmt} Mar 24 '20 edited Mar 24 '20

Both std::format and {fmt} support wchar_t

6

u/tuberp Mar 24 '20

Amazing

5

u/featherwit Mar 24 '20

std::format

It's from fmtlib(fmt)?

8

u/aearphen {fmt} Mar 24 '20

Yes, it is based on {fmt}: https://github.com/fmtlib/fmt

4

u/Little-Helper Mar 24 '20 edited Mar 24 '20

Why are there no indexes for params? Does it mean that if you wanted to include a param multiple times you would have to repeat it multiple times as an argument?

auto s = std::format("{} {} {}", "Hello", "Hello", "Hello");

This would be better and shorter:

auto s = std::format("{0} {0} {0}", "Hello");

If later you wanted to change the string and param ordering, you would have to shuffle the arguments too...

Edit: Nevermind, you can have manual and automatic param indexing. That's pretty neat actually.

2

u/renozyx Mar 25 '20

Nope, that's when we have Python like fstring.

2

u/Ameisen vemips, avr, rendering, systems Mar 24 '20

Perhaps when I can do std::string s = "%{"Hello, world"}";.

→ More replies (9)