4

I was pretty sure that the compiler is supposed to define __cplusplus such that it indicates which version of the C++ standard is being parsed.

But compiling the following code

#include <iostream>
#include <vector>

int main(int argc, char **argv)
{
  const int cxx_version = __cplusplus;
  std::cout << "C++ version: " << cxx_version << std::endl;

  std::vector<int> v(100, 2);
  int s = 0;
  for(auto i : v) {
    s += i;
  }

  std::cout << "Sum: " << s << std::endl;

  return 0;
}

with

cl cxx11.cpp /Qstd=c++11

or

icl cxx11.cpp /Qstd=c++11

with both Visual Studio 2013 and Intel C++ 16.0 on Windows, produces the unexpected result

C++ version: 199711
Sum: 200

As we can see, the range-based for loop works as expected, but __cplusplus should have been 201102, right?

So my question is three-fold:

  • Is /Qstd=c++11 the correct way to enable C++ 2011 on Microsoft or Intel compiler on Windows?
  • Is __cplusplus the correct think to check the language version based on the C++ standard? Is there a more reliable alternative for standards-compliant compilers?
  • Is there a reliable check for either the Microsoft or Intel compilers in Windows?
Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48
  • Possible duplicate of [How to determine the version of the C++ standard used by the compiler?](http://stackoverflow.com/questions/2324658/how-to-determine-the-version-of-the-c-standard-used-by-the-compiler) – GSerg Feb 17 '16 at 07:08
  • @GSerg Yes, it seems a lot of the question is duplicate, but I could still use some help specific to Microsoft and Intel compiler on Windows if possible. – Nicu Stiurca Feb 17 '16 at 07:13
  • [It's](http://stackoverflow.com/questions/14131454/visual-studio-2012-cplusplus-and-c-11) a [noted issue](https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l) ... an aside, if the code doesn't compile, then wouldn't the compiler not support C++11? I can't compile a range-based for-loop on my gcc version 4.2 (no C++0x support at all), as an example .. are you trying to enable C++11 programmatically or at compile time? – txtechhelp Feb 17 '16 at 07:23
  • 1
    @txtechhelp You might want to avoid compiling non-C++11 code for compilers that don't support it. For example, move constructors and assignment operators. – juanchopanza Feb 17 '16 at 07:33
  • 3
    You're having a somewhat binary look on "C++11 support". Formally that's right - a compiler either supports it or not - but you then go on to assume that if a compiler doesn't support C++11, then it supports none of the features. It doesn't work that way - a non-C++11 compiler can till support a subset of C++11 features such as range-based `for`. – MSalters Feb 17 '16 at 08:00
  • @juanchopanza, totally agreed :) Was more trying to get at the root of the OP's problem (since the question of can one check the `__cplusplus` macro has an answer) .. e.g. is OP trying to test for certain features of MSVC (since VS 2013 has support for C++11) – txtechhelp Feb 17 '16 at 08:09
  • 1
    Remember, `__cplusplus` just indicates the most recent version of the standard that a compiler is _fully_ compliant with. MSVS 2013, for example, [doesn't support ref-qualifiers, `constexpr`, attributes, expression SFINAE, inheriting constructors, `char16_t` or `char32_t`, Unicode string literals, `noexcept`, and more](https://msdn.microsoft.com/en-us/library/hh567368.aspx#top), so it isn't allowed to define it as anything higher than `199711L`. – Justin Time - Reinstate Monica Nov 09 '16 at 19:58
  • To see what features a given compiler supports, you could check [CPPReference](http://en.cppreference.com/w/cpp/compiler_support). MSVC 2013 is version 12.0, so just look for MSVC 12.0 or lower, and Intel C++ 16.0 or lower. – Justin Time - Reinstate Monica Nov 09 '16 at 20:01
  • I just tried on MSVS 2013, update 3, cl.exe ... /Qstd=c++11 just not work: cl : Command line warning D9002 : ignoring unknown option '/Qstd=c++11' The cl.exe version is: Microsoft (R) C/C++ Optimizing Compiler Version 18.00.30723 for x86 – zhaorufei Feb 07 '17 at 10:12
  • @zhaorufei - `/Q` is an Intel compiler option that has nothing to do with the Microsoft compiler. The current MS compiler VS2017 ver 15.3 supports /std:c++14 and /std:c++17 – Bo Persson Aug 25 '17 at 20:23

0 Answers0