| by Arround The Web | No comments

Cref C++

C++ is a high programming language that is most commonly used for game development and operating system development. It provides some useful built-in functions that make the programming in C++ much easier. These built-in functions allow you to build any kind of simple or complex application with ease. The user can perform huge functions just by including the built-in function in the program. This article will help you learn the working of the cref() built-in function of C++. Here, we will define the function of the cref() and help you understand its working by demonstrating some useful examples.

What Is Cref() C++ Function?

The cref() is a built-in function of the C++ programming language provided in the reference_wrapper standard library. It is a helper function that uses the template argument deduction for generating an object of reference_wrapper type while determining the result’s template argument.

In simple words, the cref() function is used to create a reference wrapper. The reference wrapper is a method of passing the reference of an argument to a function instead of the actual value. When the control is inside the function, the reference of the parameter is used to access the actual value, making no changes to the original value but to the reference value. This is useful when the user does not want to modify the original value but needs to use them for further calculations.

Syntax of Cref() C++ Function:

The proper syntax of the cref() function is as follows:

The cref() function takes one argument as input that needs to be wrapped and returns the reference wrapper object to hold the “const parameter” element. Now, let us explore some examples to understand the working of the cref() function better. By following the useful examples given, you can learn to implement the cref() function in your programs and develop good applications.

Example 1:

This first example of the cref() article defines the basic use of the cref() function in the C++ programming language. See the following code and understand the working of each line step by step.

The first line includes the “iostream” library in the program so that the program can use the standard input output functionality of the C++ programming language. The standard “iostream” library allows you to use the Cin, Cout, and other functions to make your program usable. The “functional” header file is a part of the standard library provided by the C++ programming language. It offers a predefined set of class templates for performing several functions. It includes creating the logic, performing comparisons, different arithmetic operations, etc. The program starts with the main() function. The complete code is written in the main() function.

The first line of code in the main() function is “int ref(9)”. It is calls the “ref” function and passes “9” as a parameter to the “ref” function. After that, the “auto ren = std::cref(ref);” is defined. This line creates a reference wrapper of the “ref”. The “ref++” adds 1 in the given parameter which is 9. So, the “ref++” becomes 10. The computed value is passed to the cout statement so that it can be printed. The return 0 statement makes sure that the output is returned successfully. If there is some error, the return 0 raises the exception.

To compile and execute the program, you need to press the “compile & run” on the taskbar. Or you can simply press the “f11” function key on the keyboard. Checkout the given output in the following:

As you can see, the output is 10 which is exactly what we calculated manually.

Example 2:

This example demonstrates the exception raised by the compiler when the user wants to make direct changes to the original reference value. You can find the code for your understanding in the following illustration.

The function uses the same libraries as used in the previous example. The “void f(const int& n)” function is defined at the start of the program. It takes the input from the main function and tries to manipulate it. The cout statement prints the value when the control is in the function.

The “n++” statement increments n with 1. In the main function, “n” is initialized with 1. The “std::function<void()> bound_f = std::bind(f, std::cref(n));” statement is used to bound the function “f” with the reference wrapper value. The two cout statements are used to print the values when the control is in the main function. The first cout statement prints the value before calling the child function. The second cout statement prints the value after calling the child function.

Press “f11” in the function keys or the compile icon on the taskbar to execute the program.

Here is the output produced by the program:

When you execute this code, you will get an error “increment of read-only reference ‘n’”. This happens because we passed the reference wrapper to the “const int& n” in the child function. The “const” does not allow you to make changes to the wrapper parameter. Hence, it raises an error.

Example 3:

The next example is a bit more complex version of the first example. Here, we define the three variables and pass them on to the child function each, differently. The first argument is passed simply without creating a reference wrapper. However, the other two input numbers are provided after creating the reference wrapper. Refer to the following given code:

The rest of the program is the same as what was given in the second example. The only difference is that we provide three inputs and not make any input constant. The bind function binds the child function with the input parameters. The output after executing the program is as follows:

Note that the values before calling the child function and when the control is in the child function are the same. However, when the control returns to the main function, all input values is incremented by 1. This happens because the increment of inputs is computed in the child function and returns to the main function.

Conclusion

In this guide, we learn about the cref() function of the C++ programming language. The cref() function is used to calculate the reference wrapper of the given input. With the help of examples, we learned how to implement the cref() function in the C++ program.

Share Button

Source: linuxhint.com

Leave a Reply