MCPcopy Create free account
hub / github.com/andreiavrammsd/cpp-channel / main

Function main

examples/move.cpp:63–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

61// Copy and move semantics with a user-defined type.
62
63int main()
64{
65 msd::static_channel<data, 10> chan{};
66
67 // l-value: will be copied
68 const auto in1 = data{1};
69 chan << in1;
70
71 // r-value: will be moved
72 chan << data{2};
73
74 // l-value -> std::move -> r-value: will be moved
75 auto in3 = data{3};
76 chan << std::move(in3);
77
78 std::vector<int> actual;
79
80 // Each value will be moved when read
81 for (const data& out : chan) {
82 std::cout << out.get_value() << '\n';
83
84 actual.push_back(out.get_value());
85
86 if (chan.empty()) {
87 break;
88 }
89 }
90
91 // Read values
92 const std::vector<int> expected{1, 2, 3};
93
94 if (actual != expected) {
95 std::cerr << "Error: got: ";
96 for (const int value : actual) {
97 std::cerr << value << ", ";
98 }
99
100 std::cerr << ", expected: ";
101 for (const int value : expected) {
102 std::cerr << value << ", ";
103 }
104 std::cerr << '\n';
105
106 std::terminate();
107 }
108
109 // 1 copy when in1 was written
110 constexpr std::size_t expected_copies = 1;
111
112 if (data::copies_ != expected_copies) {
113 std::cerr << "Error: copies: " << data::copies_ << ", expected: " << expected_copies << '\n';
114 std::terminate();
115 }
116
117 // 1 move when the second value was written
118 // 1 move when in3 was written
119 // 3 moves when the 3 values were read
120 constexpr std::size_t expected_moves = 5;

Callers

nothing calls this directly

Calls 3

emptyMethod · 0.80
get_valueMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected