blob: 291d81a15bbacfc95bcae61a0b97f63c3c43982e (
plain) (
blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#include <iostream>
// std::forward example - preserve the value category (lvalue or rvalue)
void f(const int& x)
{
std::cout << "lvalue" << std::endl;
}
void f(const int&& x)
{
std::cout << "rvalue" << std::endl;
}
template<class T>
void g(T&& x)
{
f(std::forward<T>(x));
}
struct printer
{
static void print()
{
std::cout << std::endl;
}
template<class T, class... Args>
static void print(const T& first, const Args&... args)
{
std::cout << first << " ";
print(std::forward<const Args&>(args)...);
}
};
struct Entity
{
int x;
friend std::ostream& operator<<(std::ostream& os, const Entity& e);
Entity(int x_) : x(x_) {}
Entity(Entity&)
{
std::cout << "Copied " << x << std::endl;
}
};
std::ostream& operator<<(std::ostream& os, const Entity& e)
{
os << e.x;
return os;
}
int main()
{
int x = 10;
g(x);
g(std::move(x));
}
|