I am trying the following code:
#include <iostream>
#include <vector>
struct copy_counter {
int copies = 0;
int moves = 0;
copy_counter() = default;
copy_counter(copy_counter const& rhs) noexcept : copy_counter() { copies++; }
copy_counter(copy_counter&& rhs) noexcept : copy_counter() { moves++; }
};
class base_checker {
copy_counter cc;
public:
int& copies = cc.copies;
int& moves = cc.moves;
};
struct move_only : public base_checker {
move_only() = default;
move_only(move_only const&) = delete;
move_only& operator=(move_only const&) = delete;
move_only(move_only&&) = default;
move_only& operator=(move_only&&) = default;
};
int main()
{
std::vector<move_only> v;
v.emplace_back();
v.push_back({});
v.emplace_back();
v.resize(v.capacity()+100);
std::cout << v[0].moves << ' ' << v[0].copies << "\n";
//std::cout << v[1].moves << ' ' << v[1].copies << "\n"; //#1
//std::cout << v[2].moves << ' ' << v[2].copies << "\n"; //#2
}
Question #1:
Running the above code, I get some garbage values for v[0].
Then I uncommented #1, and got for v[1] values 0 32
Question #2
Why are there 32 copies, when I would expect 0 copies and no more than a couple of moves?
Then I uncommented #2, and got for v[1] values 0 0
Question #3
Why does printing v[2] affect the values of v[1]?
http://ift.tt/2F8Sblm
http://ift.tt/2EhbZ4S
http://ift.tt/2F9Xykk
Aucun commentaire:
Enregistrer un commentaire