6
#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??

JaMiT
  • 14,422
  • 4
  • 15
  • 31
Robert Page
  • 366
  • 4
  • 10
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/225132/discussion-on-question-by-ritobroto-ganguly-what-is-move-in-c98). – Samuel Liew Nov 25 '20 at 22:46

1 Answers1

7

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.

  • 1
    Maybe interesting to note is that libc++ has always targetted c++11 and above and c++98 was added afterwards because some people needed it. – JVApen Nov 25 '20 at 19:42
  • 1
    Might also be useful to note that there is no `move()` in C++98 (outside of `char_traits::move()`) – AndyG Nov 25 '20 at 23:11