Temporary Materialization in C++ 17
Last Updated :
15 May, 2023
C++ is a popular programming language that is widely used in the development of system software, application software, game engines, and more. One of the most significant improvements in C++17 is the introduction of temporary materialization, a feature that helps reduce the complexity of code and improve performance. In this article, we will learn about this feature with the help of examples.
The emergence of temporary objects in C++17 is marked by the automatic creation of objects as needed by the language, without any explicit command to initiate their creation. Unlike the previous version, only when an expression resulted in a temporary object were such entities created; however, in C++17, their creation can occur in a broader range of circumstances.
Application of the temporary materialization
In C++, applications of temporary materialization are mentioned below:
- Prvalue (pure rvalue) expressions are passed to a function or operator that expects an lvalue reference.
- Objects that are returned from a function by value.
- Objects that are initialized using an initializer list.
Example 1:
C++
// C++ Program to demonstrate use of temporary
// materialization in C++17.
#include <iostream>
#include <string>
std::string foo() { return "Hello"; }
void bar(const std::string& str)
{
std::cout << str << std::endl;
}
int main()
{
bar(foo() + " world!");
return 0;
}
Explanation of the above program:
In this example, Upon calling the foo() function within the main() function, a fleeting std::string entity comprising the phrase 'Hello' is produced. By using the operator to combine this temporary std::string with the 'world', yet another momentary std::string entity is generated to accommodate the combined outcome. The const std::string parameter of the bar() function is supplied with an ephemeral std::string entity that is generated and sent as an input argument.
Example 2:
C++
// C++ Program to demonstrate use of use of the temporary
// materialization in C++17.
#include <iostream>
#include <string>
using namespace std;
string getName() { return "GeeksforGeeks"; }
int main()
{
cout << getName() << endl;
const string& name = getName();
cout << name << endl;
return 0;
}
OutputGeeksforGeeks
GeeksforGeeks
Explanation of the above program:
In the above example, the std::string object can be obtained through the usage of the getName() function. Specifically, by utilizing the line cout << getName() << endl; we are able to directly output the returned string value. Moreover, creating a constant reference to the getName() function's return value can be done with the line const string, thus allowing for further manipulation.
Creating a constant reference name would have been impossible if the std::string object had been destroyed before initialization, which is only possible with the presence of Temporary Materialization in C++17.
Advantages of Temporary Materialization
The advantages of using Temporary Materialization are mentioned below:
- Avoiding unnecessary object copies: With temporary materialization, it is possible to avoid making unnecessary copies of objects. This can lead to significant performance improvements, particularly when working with large objects.
- Improved code readability: Using temporary materialization can lead to more readable code. It can eliminate the need for complicated workarounds to avoid copying objects, making code more straightforward and easier to understand.
- Better type safety: By preventing the creation of temporaries of non-const references, temporary materialization can help improve type safety. This can prevent a wide range of errors and make it easier to write robust and reliable code.
- Improved code optimization: Temporary materialization can help improve code optimization by providing the compiler with additional information about the program's behavior. This can lead to more effective code generation and better performance.
Similar Reads
Partial Template Specialization in C++ In C++, template specialization enables us to define specialized versions of templates for some specific argument patterns. It is of two types: Full Template SpecializationPartial Template SpecializationIn this article, we will discuss the partial template specialization in C++ and how it is differe
3 min read
Order of Evaluation in C++17 In C++ programming, the order of evaluation of expressions can have a significant impact on the behavior and correctness of the code. C++17 introduced changes to the order of evaluation rules, providing clearer guidelines and improving consistency across different compilers. In this article, we will
4 min read
std::variant in C++ 17 In the world of modern C++ programming, the std::variant is a powerful tool that allows you to work with multiple data types in a flexible and type-safe manner. In this article, we will discuss std::variant and explore its fundamentals, applications, and benefits with practical code examples. Prereq
4 min read
Aggregate Initialization in C++ 20 C++20 has undergone several upgrades that aim to streamline and enhance the code. Among these improvements lies a remarkable modification that simplifies the initialization process of aggregates, such as arrays, structs, and classes lacking user-declared constructors. In C++20, you can use aggregate
3 min read
C++17 - <memory_resource> Header C++17 introduced the <memory_resource> header, which provides a mechanism for customizing the allocation and deallocation of memory in C++ programs. This header defines the memory_resource class and several derived classes that implement different memory allocation strategies. The C++ Standard
4 min read