std::forward in C++ Last Updated : 26 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, std::forward() is a template function used for achieving perfect forwarding of arguments to functions so that it's lvalue or rvalue is preserved. It basically forwards the argument while preserving the value type of it.std::forward() was introduced in C++ 11 as the part of <utility> header file.Syntaxstd::forward<T> (arg)where,arg: Argument to be forwarded.Example of std::forward() C++ // C++ program to illustrate the use of // std::forward() function #include <iostream> #include <utility> using namespace std; // Function that takes lvalue reference void UtiltyFun(int& i) { cout << "Process lvalue: " << i << endl; } // Overload of above function but it takes rvalue // reference void UtiltyFun(int&& i) { cout << "Process rvlaue: " << i << endl; } // Template function for forwarding arguments // to utlityFun() template <typename T> void Fun(T&& arg) { UtiltyFun(forward<T>(arg)); } int main() { int x = 10; // Passing lvalue Fun(x); // Passing rvalue Fun(move(x)); } OutputProcess lvalue: 10 Process rvlaue: 10 Explanation:Here, in function fun(), T&& is called as universal reference which means it can hold both type (lvalue and rvalue). By using std::forward(), it will check what type is coming in arg. Based on whether it is lvalue or rvalue, it will call the correct overloaded version of the utilityFun(). If we don't use std::forward, it will every time call lvalue version of utilityFun(int &i), as compiler won't check the type of arg and assumed that it is lvalue only, which we will see in below example.Above Example Without std::forward C++ // C++ Program to illustrate what will happen if we // don't use std::forward for argument passing #include <iostream> #include <utility> using namespace std; // Function with lvalue reference parameter void utiltyFun(int& i) { cout << "Process lvalue: " << i << endl; } // Overload of above function with rvalue // reference arameter void utiltyFun(int&& i) { cout << "Process rvlaue: " << i << endl; } // Function that forwards argument template <typename T> void fun(T&& arg) { utiltyFun(arg); } int main() { int x = 10; // Passing lvalue fun(x); // Passing rvalue fun(move(x)); } OutputProcess lvalue: 10 Process lvalue: 10 Things to RememberIn the above program,std::move() function will cast its arguments unconditionally, means either if we pass lvalue or rvalue it will cast it to rvalue, it doesn't move anything.std::forward will cast its argument to rvalue only if argument coming is rvalue.std::move and std::forward won't do anything in runtime. Comment More infoAdvertise with us Next Article forward_list::swap() in C++ STL S sumu4034 Follow Improve Article Tags : C++ CPP-Functions Practice Tags : CPP Similar Reads Forward List in C++ STL In C++, forward_list container provides the implementation of singly linked list data structure. It stores data in non-contiguous memory where each element points to the next element in the sequence. This makes insertion and deletion faster once the position of the element is known.Example:C++#inclu 7 min read Forward Iterators in C++ After going through the template definition of various STL algorithms like std::search, std::search_n, std::lower_bound, you must have found their template definition consisting of objects of type Forward Iterator. So what are they and why are they used ? Forward iterators are one of the five main t 6 min read forward_list::swap() in C++ STL Forward list in STL implements singly linked list. Introduced from C++11, forward lists are useful than other containers for insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from the list by the fact that forward list keeps tr 2 min read forward_list::swap() in C++ STL The forward_list::swap() is a built-in function in CPP STL which exchanges the contents of the first given forward_list with another forward_list. Syntax: swap(forward_list first, forward_list second) or forward_list1.swap(forward_list second) Parameters: The function accepts two parameters which ar 3 min read std::future in C++ The C++ Concurrency support library includes the std::future class template, which has been available since C++11. This template is defined in the <future> header and provides a means to access the outcomes of asynchronous operations. What is std::future in C++? In C++, the std::future is a cl 4 min read std::forward_list::sort() in C++ STL Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of 3 min read Like