aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/nrvo_by_default/rvo.cpp
blob: a1e73e5ae520e5f1853ae95b29de5012c3c1708f (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "Egor's blog" project.
// For details, see https://github.com/egor-tensin/blog.
// Distributed under the MIT License.

#include <iostream>
#include <utility>

namespace
{
    class C
    {
    public:
        explicit C()
        {
            std::cout << "\tC::C()\n";
        }

        C(C&&) noexcept
        {
            std::cout << "\tC::C(C&&)\n";
        }

        C(const C&)
        {
            std::cout << "\tC::C(const C&)\n";
        }

        C& operator=(C&&) noexcept
        {
            std::cout << "\tC::operator=(C&&)\n";
            return *this;
        }

        C& operator=(const C&)
        {
            std::cout << "\tC::operator=(const C&)\n";
            return *this;
        }

        ~C()
        {
            std::cout << "\tC::~C()\n";
        }
    };

    C make_rvo()
    {
        return C{};
    }

    C make_prevent_rvo()
    {
        return std::move(C{});
    }

    C make_nrvo()
    {
        C c;
        return c;
    }

    C make_prevent_nrvo()
    {
        C c;
        return std::move(c);
    }
}

int main()
{
    {
        std::cout << "C c\n";
        C c;
    }
    {
        std::cout << "C c(make_rvo())\n";
        C c(make_rvo());
    }
    {
        std::cout << "C c{make_rvo()}\n";
        C c{make_rvo()};
    }
    {
        std::cout << "C c = make_rvo()\n";
        C c = make_rvo();
    }
    {
        std::cout << "C c(make_nrvo())\n";
        C c(make_nrvo());
    }
    {
        std::cout << "C c{make_nrvo())\n";
        C c{make_nrvo()};
    }
    {
        std::cout << "C c = make_nrvo()\n";
        C c{make_nrvo()};
    }
    {
        std::cout << "C c(make_prevent_rvo())\n";
        C c(make_prevent_rvo());
    }
    {
        std::cout << "C c{make_prevent_rvo()}\n";
        C c{make_prevent_rvo()};
    }
    {
        std::cout << "C c = make_prevent_rvo()\n";
        C c = make_prevent_rvo();
    }
    {
        std::cout << "C c(make_prevent_nrvo())\n";
        C c(make_prevent_nrvo());
    }
    {
        std::cout << "C c{make_prevent_nrvo()}\n";
        C c{make_prevent_nrvo()};
    }
    {
        std::cout << "C c = make_prevent_nrvo()\n";
        C c = make_prevent_nrvo();
    }
    return 0;
}