C++

C++ std::forward

One of the essential C++ functions, the forward() function, will be covered in today’s lesson. To make sure that every aspect of the function is understood clearly, we will also use a few examples. But first, let’s quickly review the C++ programming language, the fundamentals of functions, and the logic behind the implementation of the forward() function.

The foundation of the C++ programming language is founded on the concepts of object-oriented programming (OOP). The user can easily build and understand the program’s principles since C++ has a proper framework. Moreover, since functions are concise pieces of code, the principle has been made clear in C++ so that it can be used anywhere within a running program.

Introduction

In C++ language, the forward() function works similarly to the move() function as both are used to implement move semantics. In the forward() function, it accepts a forwarding value as input value and then it determines whether an lvalue or an rvalue referenced has been supplied to the input value. And in return whether it will give the proper type of references with the T template argument or not. The forward() function is used to do perfect forwarding. When a rvalue reference is assigned to a ref variable, ref becomes a named entity. Even though ref is an rvalue reference, it falls within the lvalue category.  As a result, copying semantics rather than move semantics are employed for references.

Syntax

The lvalue() function is a left value template in the forward() function and it is the predefined keyword in C++ language which is used to begin a class template. It is then followed by any template parameters that are contained inside a  <> and the class declaration. The template parameter “T” and the keyword “class” are placeholders for the data types used in the declaration. We will pass this “T” parameter and call the forward() function and pass the type and argument in it. The remove_reference() template function is a part of the C++ standard library. The rvalue() function is the right value template in the forward() function. A ref variable turns into a named object when an rvalue reference is applied to it. Even if the ref is a reference to an rvalue, it belongs in the lvalue class.

Parameters:

arg: is an input argument and it is used to pass in the forward() function.

Return Value:

In return, we will get the rvalue(right value) reference if we didn’t get the lvalue(left value) reference.

Example 01:

Now, let us start implementing our very first example of one of the functions which is the forwarding() function in C++ language. To start writing the code which we want to implement in C++ programming Language, we always first need a compiler where we write the code and execute the code. So, for that, you can install any compiler of C++ language, or you can also use the online compiler to write and execute the code if it is compatible with the libraries you are going to use in the program.

#include <utility>

#include <iostream>

using namespace std;

After opening the C++ compiler, now you can start writing the code. In C++ programs, we always need to include header files in the program so that we can easily call the function which we are going to use in the program anywhere. These libraries are the built-in libraries of the C++ programming language. We just simply have to write only one line of code to include these libraries. The first library we will always include in the C++ program is the “iosteam” library which is used to get input from the user and we can also display the data through the “iosteam” library. To call the library, we first write the “#” symbol which will tell the compiler that we are going to access the library. Then, we will write the “include” keyword which is the predefined keyword that will instruct the compiler that we are including the library. And in the <> symbol, we will write the name of the library we are importing in the program which is iostream”.

We will include the second built-in library of the program which we are using that is “utility”. The “utility” library is used to define some templates that are used often across the Standard Template Library(STL) in the whole existing program. Furthermore, we were using the “using namespace std” rule to prevent objects, methods, and parameters from sharing the same domain all across the rest of the program.

void overloaded (int const &A)
{
cout<< "[lvalue]";
}

void overloaded (int &&A)
{
cout<< "[rvalue]";
}

template
T forward (T && A)
{
   overloaded (A);
   overloaded (forward(A));
}

We implemented the global function which is one of the object-oriented single methods where we declared the function outside the main() function and then we just simply call the function in the main() function and passed the declared variables in it so that we get the desired output. First, we will write the data type of the function which we are implementing that is “void” which means that the function doesn’t return any value.

Next, we will write the function name which we are going to implement “overload() function” ans and we will pass the parameter in it. In the overload() function we have printed the message b using the cout() method. The overloaded () function means that you can provide multiple functions with the same name in the same namespace.   With overloaded functions, you can give a method multiple semantics based on the types and amounts of its parameters. As you see we have implemented one more function with the same name “overloaded” in the same program and we have passed the parameter in it.

Next, we have implemented the predefined template() function in the program so that we can return the result of any data type. And we called the overloaded() function in it. As you have seen, the overloaded() function is “int” type so that’s why we have implemented the template() function, if we input the value of any type so that the compiler doesn’t generate any error in the program.

Then, we will start the main() function where we will declare the variable and we will access the above global functions here. First, we have declared the two variables of int type and assigned the value to them. We have declared another variable of type “int” named and we have stored the add expression in “res”. Then, we printed the “res” variable and also passed the “res” variable in the forward() function so that we get the lvalue and in the next forward() function call we will get the both lvalue and rvalue reference.

int main () {
   int a = 30, b = 23;
   int res = a+b;

cout<< res << " is a: ";
   forward (res);
cout<<endl;
cout<<a+b<< " is a: " ;
   forward (a+b);
cout<<endl;
   return 0;
}

And at the end of the main() function, we will return 0 to the main() function so that it will tell the compiler that we want to stop the execution of the program and show the output of the program.

Conclusion

In this article, we have learned about the forward() function which is the built-in function of the C++ programming language. We have also learned about what overloading is and also about the template() function of the C++ language. We have implemented an example of the forward() function and we have used the template() function in it.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.