6

I'm trying to set up c++11 to my project on visual studio, and to find out which version the compiler used by default I used the following code:

#include <iostream>

int main(){
        #if __cplusplus==201402L
        std::cout << "C++14" << std::endl;
        #elif __cplusplus==201103L
        std::cout << "C++11" << std::endl;
        #elif __cplusplus==199711L
        std::cout << "C++" << std::endl;
        #elif __cplusplus==201703L
        std::cout << "C++17" << std::endl;
        #elif __cplusplus==1
        std::cout << "C+" << std::endl;
        #endif

        int* i = nullptr;

        return 0;
}

Having output c++ (98) I tried to force the compiler to use c++11 via the CMakeLists like this:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

The output was always C ++ (98), so I added

int* i = nullptr;

and amazingly the output remains c++ (98) but the application works without problems. How is this "anomaly" explained and how do I know / decide which standard to use?

G. De Mitri
  • 115
  • 10
  • 2
    Don't use `==`. You will miss any versions that happen in between. Use `>=`. For Visual Studio, you may instead need to test `_MSC_VER`. For example, my C++11 test looks like this: `#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900)` – paddy Oct 19 '20 at 22:46
  • 1
    More information about the probable underlying issue is here: https://stackoverflow.com/questions/14131454/visual-studio-2012-cplusplus-and-c-11 – paddy Oct 19 '20 at 22:50
  • OK thanks a lot for the reply and for sharing the link – G. De Mitri Oct 19 '20 at 23:04
  • Do note that depending on the version of MSVS, you might not be able to get C++11. In MSVS 2019 for example the lowest you can go is C++14. – NathanOliver Oct 20 '20 at 01:42

1 Answers1

3

According to this question: Visual Studio 2012 __cplusplus and C++ 11 , provided by @paddy, this is a known bug with MSVC in that the version macro is set to C++98. You should compile with the /Zc:__cplusplus switch to change the version macro to the correct value.

Anonymous1847
  • 2,568
  • 10
  • 16
  • 1
    _You should compile with the /Zc:__cplusplus switch to change the version macro to the correct value._ Yuk (not taking a pop at you, but yuk). – Paul Sanders Oct 19 '20 at 23:02
  • @PaulSanders Microsoft development products in particular are full of artifacts and bugs that weren't done correctly or standard-conformingly the first time, and then stuck around because of some promise of backwards-compatibility. – Anonymous1847 Oct 21 '20 at 22:36