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

    int main()
    {
     if (__cplusplus == 201703L) std::cout << "C++17\n";
     else if (__cplusplus == 201402L) std::cout << "C++14\n";
     else if (__cplusplus == 201103L) std::cout << "C++11\n";
     else if (__cplusplus == 199711L) std::cout << "C++98\n";
     else std::cout << "pre-standard C++\n";

     vector<int> a;
     a.push_back(3);
     vector<int> b = move(a);
    }

As far as I know, "move semantics" is a part of c++11

Output(when compiled with clang++ command without any flags): C++98

Then how are the move semantics working??

I know that I can select the particular version of c++ I want to compile to with the -std=c++xx flag, but that's not what I want.

I want to know, what is the default version of c++ to which clang compiles to in macOS ?

Robert Page
  • 366
  • 4
  • 10
  • 1
    You can check in code: https://stackoverflow.com/a/7132549/362589 – Daniel Nov 25 '20 at 15:21
  • 2
    The warning text would be nice. Along with version numbers. clang has compiled to C++14 for a good while. I know macOS hides what version they're actually using, but I haven't had this kind of issue. – sweenish Nov 25 '20 at 15:25
  • @sweenish, I have updated the post. And if it is compiling to c++14 I don't think it should give the error I have shown in the image in my question. – Robert Page Nov 25 '20 at 15:30
  • It's worth mentioning that any project worth its salt will specify the C++ version to avoid these exact issues. Relying on the default is only a choice for toy programs. – sweenish Nov 25 '20 at 15:45
  • @Dani, your link was very helpful. The default version my clang is compiling too is c++98, and the move() function is present in c++98 as well, but apparently it does not perform move semantics, but just deep copying. – Robert Page Nov 25 '20 at 15:46

2 Answers2

1

The default version my clang is compiling too is c++98, and the move() function is present in c++98 as well, but apparently it does not perform move semantics(i.e. create Rvalue Reference), but just deep copying.

The following link gives answer to finding out to which version of c++ the compiler is compiling to:

https://stackoverflow.com/a/7132549/362589

Here is the link to the answer of what is move() in c++98 :

https://stackoverflow.com/a/65010079/13023201

Robert Page
  • 366
  • 4
  • 10
1

clang has an option to specify the C++ version you want to use: - std=xxx.

Just like gcc, you can ask for the ISO or the GNU version. Eg:

c++98
c++03
ISO C++ 1998 with amendments
gnu++98
gnu++03
ISO C++ 1998 with amendments and GNU extensions

It seems like clang uses the GNU flavor by default:

The default C++ language standard is gnu++14.

gcc does the same.

It is likely that you compiler uses gnu98 by default, which means your version of C++ is indeed C++98 (== C++03), but extensions provide additional features such as std::move.

You can go to compiler explorer and test the following code:

#include <iostream>

int main() {
    std::cout << __cplusplus;
}

Compiled with x86-64 clang 11.0.0 using -std=gnu++98, the program outputs:

199711

You can check from you code if the ISO or the GNU version if used thanks to the STRICT_ANSI macro:

Differences between all c* and gnu* modes:

c* modes define “STRICT_ANSI”.

(...)

Bktero
  • 722
  • 5
  • 15