๐++17 ๐๐จ๐ฉ๐ฒ ๐๐ฅ๐ข๐ฌ๐ข๐จ๐ง and ๐ญ๐๐ฆ๐ฉ๐จ๐ซ๐๐ซ๐ฒ ๐ฆ๐๐ญ๐๐ซ๐ข๐๐ฅ๐ข๐ณ๐๐ญ๐ข๐จ๐ง
๐ฏ 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
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.