Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is move() in 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;
}

Output (in c++98):

b: 0x7f9a82405730
a: 0x7f9a82405720

Output (in c++11):

b: 0x7f9a82405730
a: 0x0

I am using Apple clang 11.0.3.

No compiler flags are used for the first output.

-std=c++11 flag for the second output.

I know what move() does in c++11 (and higher versions). But as I can see using move() in c++98 does nothing to the object passed and just deep copy happens.

Then why is there a move() in c++98??

like image 973
Robert Page Avatar asked Nov 25 '20 16:11

Robert Page


1 Answers

The reason that you can call std::move at all pre-C++11 is that libc++ doesn't wrap its implementation of it in #if _LIBCPP_STD_VER >= 11. This doesn't work in libstdc++ (which Linux uses by default) because it guards std::move with #if __cplusplus >= 201103L.

As for why using it doesn't make a.data() be null, it's because libc++ does wrap the move constructor in #ifndef _LIBCPP_CXX03_LANG, so it falls back to the copy constructor.

like image 142
Joseph Sible-Reinstate Monica Avatar answered Sep 17 '22 18:09

Joseph Sible-Reinstate Monica