jeudi 21 novembre 2019

clang what does -Wc++11-extensions flag actually do?

System Info:

  • Mac OSX 10.14.6
  • clang++: Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  • gcc version 9.2.0 (Homebrew GCC 9.2.0_1)

In a modern C++ programming course, in the examples on l-values and r-values the following code is given to demonstrate r-value references:

#include <iostream>

void Print(int &x) {
  std::cout << "Print(int&)" << std::endl;
}

void Print(const int &x) {
  std::cout << "Print(const int&)" << std::endl;
}

void Print(int &&x) {
  std::cout << "Print(int&&)" << std::endl;
}

int main() {
  int x = 10;
  Print(x);
  Print(3);
  return 0;
}

The course is demonstrated in Windows Visual Studio, but I am running the code on Mac OSX

No matter how I compile the code in these examples, operator overloading correctly uses the the r-value reference version of Print when call with Print(3). However, I do get a warning about C++11 extensions if I use clang++ without explicitly specifying the C++11 standard with the -Wc++11-extensions flag. My question is, since this code runs as expected with or without the flag, what does using this flag do for me? Is there other code that would not run correctly without the flag? I know by default this version of Clang uses the C++14 standard. What do I need the flag for?

Using g++-9 from homebrew does not complain.

Using gcc, no complaints

>>> g++-9 lrvalues.cpp -o lrvalues
>>> ./lrvalues 
Print(int&)
Print(int&&)

using default c++, complaints, but correct behavior

>>> c++ lrvalues.cpp -o lrvalues
lrvalues.cpp:11:16: warning: rvalue references are a C++11 extension [-Wc++11-extensions]
void Print(int &&x) {
               ^
1 warning generated.
>>> ./lrvalues 
Print(int&)
Print(int&&)

Using C++11 flags with the c++11 extensions flag, no complaints (what difference does it make other than to make the warnings go away)

>>> c++ lrvalues.cpp -o lrvalues -std=c++11 -Wc++11-extensions
>>> ./lrvalues 
Print(int&)
Print(int&&)
>>> 

Aucun commentaire:

Enregistrer un commentaire