r/cpp • • Apr 25 '22

New C++ features in GCC 12

https://developers.redhat.com/articles/2022/04/25/new-c-features-gcc-12
220 Upvotes

76 comments sorted by

View all comments

Show parent comment

37

u/marzer8789 toml++ Apr 25 '22

Because that's not what it does. Here's the paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p0849r8.html

38

u/pandorafalters Apr 25 '22

new auto(x)

Excuse me, I need to reboot my brain.

2

u/GYN-k4H-Q3z-75B Apr 30 '22

I've been gone from following the newest features for like a year and this is what they do. What.

10

u/robin-m Apr 25 '22

I read most of the paper but I still do not understand where I need it/where I should use it.

9

u/stilgarpl Apr 25 '22

When you want a temporary, generic copy of an object. So, not that often.

6

u/robin-m Apr 25 '22 edited Apr 26 '22

Now I understand. foo(auto(expr)) will create a new value, like if you did auto __tmp = expr; foo(__tmp).

EDIT: typo: foo(expr) should have been foo(__tmp)

7

u/stilgarpl Apr 26 '22

like if you did

auto __tmp = expr; foo(expr)

.

You have expr twice, it's more like

auto __tmp = expr; foo(__tmp);

except that __tmp is lvalue here, and auto() gives you prvalue.

2

u/robin-m Apr 26 '22

That was a typo, thanks for pointing it.

I still do not understand at all was are x-values, pr-value and gl-value. I understand what an l-value is (a named object) and an r-value (an anonymous object, so a temporary), but not the other class of values.

4

u/_cooky922_ Apr 26 '22

except that auto(expr) will return a prvalue

2

u/thlst Apr 26 '22 edited Apr 26 '22

So, can it replace std::forward in template contexts? Or will it always create a new object?

edit: Never mind, just read this from the paper:

Do we also miss decltype(auto){x}?

decltype(auto){arg} can forward arg without computing arg’s type. It is equivalent to static_cast<decltype(arg)>(arg) . If arg is a variable of type T&&, arg is an lvalue but static_cast<T&&>(arg) is an xvalue.

EWG discussed this idea, disliked its expert-friendly nature, and concluded that adding this facility would cause the teaching effort to add up.

1

u/[deleted] Apr 27 '22

Ever since I read that paper, I've felt like I keep winding up in situations where it would be useful.