vector,move semantics,nothrow和g ++ 4.7

sur*_*esh 5 c++ move stdvector c++11

我编写了以下代码来理解移动语义.它在g ++ - 4.6中按预期工作(即没有副本,只有移动)但在g ++ - 4.7.0中没有.我认为这是链接在g ++ - 4.7.0中的一个错误,但是这个链接说它不是g ++ - 4.7中的错误.所以,正如我从上面的链接中所理解的那样,我使移动构造函数无法生成,但它仍然只复制.但是,如果我将复制构造函数设为nothrow,则只进行移动.任何人都能解释一下吗?

#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;
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*ams 5

你是如何"让移动构造函数不被人"的?使用g ++ 4.7,如果我用移动构造函数注释,noexcept那么你的示例只会移动:

S(S&& x) noexcept{ ... }

no of moves = 25
no of copies = 0
Run Code Online (Sandbox Code Playgroud)