小编Rit*_*uly的帖子

c++98 中的 move() 是什么?

#include <iostream>
#include <vector>
using namespace std;

int main(void){

 vector<int> a;
 a.push_back(3);
 vector<int> b = move(a);
 cout<<"b: "<<b.data()<<endl;
 cout<<"a: "<<a.data()<<endl;

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出(在 c++98 中):

#include <iostream>
#include <vector>
using namespace std;

int main(void){

 vector<int> a;
 a.push_back(3);
 vector<int> b = move(a);
 cout<<"b: "<<b.data()<<endl;
 cout<<"a: "<<a.data()<<endl;

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出(在 C++11 中):

b: 0x7f9a82405730
a: 0x7f9a82405720
Run Code Online (Sandbox Code Playgroud)

我正在使用 Apple clang 11.0.3。

第一个输出不使用编译器标志。

-std=c++11 第二个输出的标志。

我知道 move() 在 c++11(和更高版本)中做什么。但是正如我所看到的,在 c++98 中使用 move() 对传递的对象没有任何作用,只会发生深拷贝。

那为什么 c++98 中有 move() 呢??

c++ move c++11 c++98

6
推荐指数
1
解决办法
167
查看次数

对函数的调用是不明确的,但为什么呢?

#include <iostream>
using namespace std;

void x(int a,int b){
  cout<<"int int"<<endl;
}
void x(char a,char b){
  cout<<"char char"<<endl;
}

int main() {
  int a =2;char c ='a';
  x(a,c);
  return 0; 
}
Run Code Online (Sandbox Code Playgroud)

在 apple clang 编译器中对“x”的调用不明确,为什么?

对于 x(int,int),第一个参数是直接匹配,第二个是对 x(char, char) 的提升,第一个参数是我知道的标准转换,也根据这个答案-> https://stackoverflow.com /a/28184631/13023201

并且提升应该优先于 std 转换,然后应该调用 x(int,int) 。那为什么会这么暧昧呢??

c++ overloading ambiguity clang

4
推荐指数
1
解决办法
129
查看次数

标签 统计

c++ ×2

ambiguity ×1

c++11 ×1

c++98 ×1

clang ×1

move ×1

overloading ×1