Question

I installed gcc4.9 using the steps mentioned in the SO post here. I was using the latest feature std::exchange() utility function which is introduced in C++14.

#include<list>
#include<utility>
int main() {
    std::list<int> lin{5,6,7,8,9};
    auto lout = std::exchange(lin, {1,2,3,4});
   return 0;
}

I performed following steps to compile the above sample program and got the following compilation error. After sometime I realized that (as there is no warning/hint by compiler message) this feature has been added in the C++14 standard so I need to use -std=c++1y here.

$g++ -std=c++11 main.cpp

main.cpp: In function ‘int main()’:
main.cpp:5:14: error: ‘exchange’ is not a member of ‘std’
  auto lout = std::exchange(lin, {1,2,3,4});
              ^

If we use the C++11 standard feature and does not provide -std=c++11, then GCC gives warning message/hint that your program is using the feature which is introduced in the C++11 as below:

main.cpp:4:21: warning: extended initializer lists only available with
-std=c++11 or -std=gnu++11
  std::list<int> lin{5,6,7,8,9};

This message is great and lets the user distinguish between the actual compilation error message and not including -std=c++11 option.

However while using gcc4.9 for C++1y feature under -std=c++11, there is no such warning message/hint? I wanted to know what could be the possible reason for this?.

Was it helpful?

Solution

The error/warning about "extended initializer lists" is emitted by the C++ parser. The C++ parser apparently knows how to parse that syntactic construct, understands it and can provide a sensible error/warning message.

With the function, the situation is a little bit different. The GCC proper does not contain knowledge about each and every standard function. For some functions it does, but for most functions it doesn't.

From the compiler proper point of view, std::exchange is just an unknown identifier, the compiler does not contain special knowledge about the standard function std::exchange, and, hence, treats it as any other unknown identifier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top