Search code examples
c++visual-c++c++17visual-studio-2019

How to correctly set _MSVC_LANG value?


As you may know _MSVC_VALUE determines whether _HAS_CXX17 and _HAS_CXX20 macros are set. Today I tried to compile the following code in Visual Studio 2019 (latest 16.6.4 version):

#include <algorithm>
#include <execution>
#include <vector>

int main()
{
    std::vector<int> _vector = { 3, 2, 1 };
    std::sort(std::execution::par, _vector.begin(), _vector.end());
    return 0;
}

Unfortunately, it throws errors. For example this one: C3083 'execution': the symbol to the left of a '::' must be a type

When I looked into the <execution> header file I noticed that it doesn't compile because macro _HAS_CXX17 is set to 0.

#if !_HAS_CXX17
#pragma message("The contents of <execution> are available only with C++17 or later.")
#else // ^^^ !_HAS_CXX17 / _HAS_CXX17 vvv

So then I looked at the definition of the _HAS_CXX17 macro which is in the vcruntime.h file:

#if !defined(_HAS_CXX17) && !defined(_HAS_CXX20)
    #if defined(_MSVC_LANG)
        #define _STL_LANG _MSVC_LANG
    #elif defined(__cplusplus) // ^^^ use _MSVC_LANG / use __cplusplus vvv
        #define _STL_LANG __cplusplus
    #else  // ^^^ use __cplusplus / no C++ support vvv
        #define _STL_LANG 0L
    #endif // ^^^ no C++ support ^^^

    #if _STL_LANG > 201703L
        #define _HAS_CXX17 1
        #define _HAS_CXX20 1
    #elif _STL_LANG > 201402L
        #define _HAS_CXX17 1
        #define _HAS_CXX20 0
    #else // _STL_LANG <= 201402L
        #define _HAS_CXX17 0
        #define _HAS_CXX20 0
    #endif // Use the value of _STL_LANG to define _HAS_CXX17 and _HAS_CXX20

    #undef _STL_LANG
#endif // !defined(_HAS_CXX17) && !defined(_HAS_CXX20)

To my surprise the value of _MSVC_LANG is set to 201402L. It should be a lot higher. I set the -std=c++17 compilation flag like in this answer. Yeah, it's my answer, which proves it worked back in May.

I tried defining the correct value for the macros myself but they are ignored or it throws some other errors:

#define _MSVC_LANG 201704L

// same code as before
// result: macro is ignored, no change
#define _HAS_CXX17 1
#define _HAS_CXX20 1

// same code as before
// result: 250+ errors like this one:
// E0457    "basic_string_view" is not a function or static data member

Just before the update I installed a separated version of standard library from here. The GCC 10.1.0. version and I put mingw/bin on the system path in Windows 10.

I am not aware that installing gcc could break msvc compiler. Then it might be caused by the update of VS 2019 to version 16.6.4?

I also looked at this question but it's not of any help.

  • Can anyone report similar problem?
  • Does anybody know how to fix this and compile code under C++17 with VS 2019 version 16.6.4?

Solution

  • For future readers:

    If you have similar issue, double-check that you are changing configuration that you will run then. In my case I was changing all the settings for Release but I tried to run the Debug configuration instead and I somehow didn't notice it.

    Steps to change the configuration:

    • Right click on project
    • In the top left corner there is a drop-down menu
    • Select the same configuration you are running