๐‚++17 ๐œ๐จ๐ฉ๐ฒ ๐ž๐ฅ๐ข๐ฌ๐ข๐จ๐ง and ๐ญ๐ž๐ฆ๐ฉ๐จ๐ซ๐š๐ซ๐ฒ ๐ฆ๐š๐ญ๐ž๐ซ๐ข๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง

SamiraSamira
2 min read

๐ŸŽฏ In this post, Iโ€™d like to discuss ๐‚++17 ๐œ๐จ๐ฉ๐ฒ ๐ž๐ฅ๐ข๐ฌ๐ข๐จ๐ง and ๐ญ๐ž๐ฆ๐ฉ๐จ๐ซ๐š๐ซ๐ฒ ๐ฆ๐š๐ญ๐ž๐ซ๐ข๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง topics I find interesting!

๐Ÿ”— ๐‹๐ข๐ง๐ค ๐ญ๐จ ๐ญ๐ก๐ž ๐ž๐ฑ๐š๐ฆ๐ฉ๐ฅ๐ž:
https://lnkd.in/gTwHw9Ef

Consider the code example that compiles in C++17 and later compilers, despite the deletion of copy and move constructors.
โ›” However, this code would not compile in ๐ฉ๐ซ๐ž-๐‚++17 ๐œ๐จ๐ฆ๐ฉ๐ข๐ฅ๐ž๐ซ๐ฌ. Even though Return Value Optimization (๐‘๐•๐Ž) is applied, the presence of a copy or move constructor is still required.

๐Ÿค” ๐–๐ก๐ฒ ๐ข๐ฌ ๐ญ๐ก๐š๐ญ ๐ญ๐ก๐ž ๐œ๐š๐ฌ๐ž?
๐Ÿ”Ž To understand the underlying reason, it is essential to recognize how C++17 clarified the semantic meaning of one of the primary value categories: ๐ฉ๐ซ๐ฏ๐š๐ฅ๐ฎ๐ž๐ฌ("pure readable values")

โžก ๐๐ซ๐ข๐จ๐ซ ๐ญ๐จ ๐‚++17,
Prior to C++17, prvalues (particularly of class type) always implied a temporary.
For instance in our example, ๐‘ฟ{} produced a temporary of type ๐˜Ÿ.
โžก ๐–๐ข๐ญ๐ก ๐‚++17 :
prvalues are expressions for short-living values used ๐Ÿ๐จ๐ซ ๐ข๐ง๐ข๐ญ๐ข๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง๐ฌ.
For example the prvalue ๐‘ฟ{} itself does not produce a temporary. Instead, it initializes an object determined by the context: In our example, this object is referred to as ๐’™.

๐Ÿ’ก So, when prvalues are used for initializations, ๐ง๐จ ๐œ๐จ๐ฉ๐ฒ ๐จ๐ซ ๐ฆ๐จ๐ฏ๐ž ๐จ๐ฉ๐ž๐ซ๐š๐ญ๐ข๐จ๐ง๐ฌ ๐š๐ซ๐ž ๐ž๐ฏ๐ž๐ซ ๐œ๐จ๐ง๐ฌ๐ข๐๐ž๐ซ๐ž๐. This is not merely an optimization; ๐˜ช๐˜ต ๐˜ช๐˜ด ๐˜ข ๐˜จ๐˜ถ๐˜ข๐˜ณ๐˜ข๐˜ฏ๐˜ต๐˜ฆ๐˜ฆ๐˜ฅ ๐˜ง๐˜ฆ๐˜ข๐˜ต๐˜ถ๐˜ณ๐˜ฆ ๐˜ฐ๐˜ง ๐˜ต๐˜ฉ๐˜ฆ ๐˜ญ๐˜ข๐˜ฏ๐˜จ๐˜ถ๐˜ข๐˜จ๐˜ฆ. As a result, we pass around the values of unmaterialized objects for initialization and this happens especially when a prvalue is passed to or returned from a function ๐’ƒ๐’š ๐’—๐’‚๐’๐’–๐’†.

๐Ÿค”So now what is ๐ญ๐ž๐ฆ๐ฉ๐จ๐ซ๐š๐ซ๐ฒ ๐ฆ๐š๐ญ๐ž๐ซ๐ข๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง?

๐Ÿ”„C++17 introduced the concept of materialization (of a temporary): the moment when a prvalue becomes a temporary object. This process is called ๐š ๐ญ๐ž๐ฆ๐ฉ๐จ๐ซ๐š๐ซ๐ฒ ๐ฆ๐š๐ญ๐ž๐ซ๐ข๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง ๐œ๐จ๐ง๐ฏ๐ž๐ซ๐ฌ๐ข๐จ๐ง, transforming a prvalue into an xvalue. Whenever a prvalue appears in a context that ๐ซ๐ž๐ช๐ฎ๐ข๐ซ๐ž๐ฌ ๐š ๐ ๐ฅ๐ฏ๐š๐ฅ๐ฎ๐ž (such as an lvalue or xvalue), a temporary object is formed and initialized with the prvalueโ€”since prvalues are primarily used for initializations in C++17. This conversion turns the prvalue into an xvalue, effectively representing the temporary.
๐๐จ๐ญ๐ž ๐ญ๐ก๐š๐ญ the materialization process doesn't introduce extra copies or new objects.

I hope you found this discussion on a nuanced topic in C++ both informative and enjoyable! ๐Ÿ˜Š ๐ŸŒธ

โžก ๐‘๐ž๐ฌ๐จ๐ฎ๐ซ๐œ๐ž๐ฌ:
C++17 The Complete Guide ๐˜•๐˜ช๐˜ค๐˜ฐ๐˜ญ๐˜ข๐˜ช ๐˜‘๐˜ฐ๐˜ด๐˜ถ๐˜ต๐˜ต๐˜ช๐˜ด
C++ Move Semantics The Complete Guide ๐˜•๐˜ช๐˜ค๐˜ฐ๐˜ญ๐˜ข๐˜ช ๐˜‘๐˜ฐ๐˜ด๐˜ถ๐˜ต๐˜ต๐˜ช๐˜ด
C++ Templates The Complete Guide 2nd Edition ๐˜•๐˜ช๐˜ค๐˜ฐ๐˜ญ๐˜ข๐˜ช ๐˜‘๐˜ฐ๐˜ด๐˜ถ๐˜ต๐˜ต๐˜ช๐˜ด, ๐˜‹๐˜ข๐˜ท๐˜ช๐˜ฅ ๐˜๐˜ข๐˜ฏ๐˜ฅ๐˜ฆ๐˜ท๐˜ฐ๐˜ฐ๐˜ณ๐˜ฅ๐˜ฆ, ๐˜‹๐˜ฐ๐˜ถ๐˜จ๐˜ญ๐˜ข๐˜ด ๐˜Ž๐˜ณ๐˜ฆ๐˜จ๐˜ฐ๐˜ณ

#cpp #cplusplus #cpp17 #coding #softwaredevelopment #interviews #programming #programmingtips #softwareengineer #learncpp #IloveCpp

0
Subscribe to my newsletter

Read articles from Samira directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Samira
Samira

Iโ€™m a backend C++ engineer passionate about C++ and multi-threaded programming. I enjoy learning and solving complex challenges with C++. Currently, I work at a leading biotechnology research company, developing and optimizing multi-threaded applications to support advanced research.