forked from furkankirac/cs409-2023-24-spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
week7-app1.cpp
48 lines (31 loc) · 1.01 KB
/
week7-app1.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
46
47
// parameter type binding exercises
#include <iostream>
template<typename> struct TypeDisplayer;
struct Widget { };
// (1) function with l-value ref
void f(Widget&) { std::cout << "1\n"; }
// (2) function with l-value ref-to-const
void f(const Widget&) { std::cout << "2\n"; }
// (3) function with r-value ref
void f(Widget&&) { std::cout << "3\n"; }
// (4) function with r-value ref-to-const
void f(const Widget&&) { std::cout << "4\n"; }
// (5) function template with forwarding ref
template<typename T>
void f(T&&) { std::cout << "5\n"; }
// (6) function template with r-value ref-to-const
template<typename T>
void f(const T&&) { std::cout << "6\n"; }
Widget getWidget() { Widget w; return w; }
const Widget getConstWidget() { const Widget w; return w; }
int main(int argc, char* argv[])
{
// what is the preference order of above functions for below questions?
Widget w1{}; // auto w1 = Widget{};
f(w1);
const Widget w2{};
f(w2);
f(getWidget());
f(getConstWidget());
return 0;
}