【问题标题】:What is move() in c++98?c++98 中的 move() 是什么?
【发布时间】:2020-11-25 16:49:06
【问题描述】:
#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;
}

输出(c++98):

b: 0x7f9a82405730
a: 0x7f9a82405720

输出(c++11):

b: 0x7f9a82405730
a: 0x0

我正在使用 Apple clang 11.0.3。

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

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

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

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

【问题讨论】:

标签: c++ c++11 move c++98


【解决方案1】:

在 C++11 之前您可以调用 std::move 的原因是 libc++ 不会将 its implementation of it 包装在 #if _LIBCPP_STD_VER &gt;= 11 中。这在 libstdc++(Linux 默认使用)中不起作用,因为it guards std::move with #if __cplusplus &gt;= 201103L

至于为什么使用它不会使a.data()为null,是因为libc++ does wrap the move constructor in #ifndef _LIBCPP_CXX03_LANG,所以它回退到复制构造函数。

【讨论】:

  • 可能有趣的是,libc++ 一直针对 c++11 及以上版本,后来添加了 c++98,因为有些人需要它。
  • 可能还需要注意 C++98 中没有 move()char_traits&lt;T&gt;::move() 之外)
猜你喜欢
  • 2020-08-06
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多