I wrote the following code to understand the semantics of movement. It works as expected (i.e., without copies and only movements) in g ++ - 4.6, but not in g ++ - 4.7.0. I thought this was a bug in the link in g ++ - 4.7.0, but the link says it was not a bug in g ++ - 4.7. So, as I understood from the link above, I made the nothrow move constructor, but still it only makes copies. However, if I create a nothrow instance constructor, only the moves take up space. Can anyone explain this?
#include <iostream> #include <vector> using namespace std; struct S{ int v; static int ccount, mcount; S(){} //no throw constructor //S(nothrow)(const S & x){ S(const S & x){ v = x.v; S::ccount++; } S(S&& x){ v = x.v; S::mcount++; } }; int S::ccount = 0; int S::mcount = 0; int main(){ vector<S> v; S s; for(int i = 0; i < 10; i++) { v.push_back(std::move(s)); } cout << "no of moves = " << s.mcount << endl; cout << "no of copies = " << s.ccount << endl; return 0; }
" move nothrow"? g++ 4.7, noexcept, :
noexcept
S(S&& x) noexcept{ ... } no of moves = 25 no of copies = 0