-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConstructorCallOrder.cpp
70 lines (59 loc) · 1011 Bytes
/
ConstructorCallOrder.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cassert>
#include <memory>
template <typename T>
struct SubWidget
{
SubWidget()
{
std::cout << "SubWidget ctor\n";
}
SubWidget(T i)
: m_val(i)
{
std::cout << "SubWidget ctor(" << m_val << ")\n";
}
SubWidget(const SubWidget& other)
: m_val(other.m_val)
{
std::cout << "SubWidget copy ctor called\n";
}
SubWidget(SubWidget&& other)
: m_val(other.m_val)
{
std::cout << "SubWidget move ctor called\n";
}
T m_val;
};
template <typename T>
struct Widget
{
Widget()
{
std::cout << "Widget ctor()\n";
}
Widget(const Widget& w)
: m_member(w.m_member)
{
std::cout << "Widget copy ctor called\n";
}
Widget(Widget&& w)
: m_member(std::move(w.m_member))
{
std::cout << "Widget move ctor called\n";
}
SubWidget<T> m_member {1};
};
template <typename T>
static Widget<T> CreateWidget()
{
auto w = Widget<T>();
w.m_member.m_val = 10;
return w;
}
int main()
{
auto w = CreateWidget<int>();
assert(w.m_member.m_val == 10);
return 0;
}