xml*_*lmx 3 c++ standards language-design move-semantics c++11
#include <vector>
using namespace std;
void f(const vector<int>&) {}
void f(vector<int>&&) {}
int main()
{
{
vector<int> coll;
//
// coll is dying, so,
// "f(coll)" will call "f(const vector<int>&)" or
// "f(vector<int>&&)" as per C++11?
//
f(coll);
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,coll正在死亡; 那么,f(coll)会打电话f(const vector<int>&)还是f(vector<int>&&)按照C++ 11?
如果f(coll)调用f(vector<int>&&)而不是f(const vector<int>&)那将违反标准,因为它会选择错误的函数重载.
如果呼叫的解决方案根据呼叫的位置以及呼叫使用后是否有任何后续语句而有所不同,那么这也会相当混乱coll.
仅对返回值给予特殊处理:
如果
expression是左值表达式并且满足或将满足复制省略的条件,除了表达式命名函数参数,那么重载决策以选择用于初始化返回值的构造函数执行两次:首先就像表达式一样是一个右值表达式(因此它可以选择移动构造函数或引用const的复制构造函数),如果没有合适的转换可用,则第二次执行重载解析,使用左值表达式(因此它可以选择复制构造函数对非常量的引用).