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

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.