-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForward.cpp
45 lines (37 loc) · 1.19 KB
/
Forward.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* \file Forward.cpp
* \brief
*
* \todo
*/
#include <StdTest/StdTest.h>
#include <Stl.h>
//-------------------------------------------------------------------------------------------------
// function with lvalue and rvalue reference overloads:
void overloaded (const int& ) {std::cout << "[lvalue]";}
void overloaded (int&& ) {std::cout << "[rvalue]";}
//-------------------------------------------------------------------------------------------------
// function template taking rvalue reference to deduced type:
template <class T> void
fn(T&& x)
{
overloaded (x); // always an lvalue
overloaded (std::forward<T>(x)); // rvalue if argument is rvalue
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
int a;
std::cout << "calling fn with lvalue: ";
fn(a);
std::cout << '\n';
std::cout << "calling fn with rvalue: ";
fn(0);
std::cout << '\n';
return 0;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
calling fn with lvalue: [lvalue][lvalue]
calling fn with rvalue: [lvalue][rvalue]
#endif