5

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?
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
  • 2
    You go into the compiler options and you set the C++ language version to C++20. You don't mess with the built-in macros. – user253751 Jul 15 '20 at 13:52
  • `/std:c++17` … You're trying to use the g++/clang switch for c++17, which isn't valid in MCSV. (unless this was just a typo in your question) – ChrisMM Jul 15 '20 at 13:54

3 Answers3

4

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
sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
2

I had the same problem trying to move to C++20 and the _MSVC_LANG kept on being 201703 (leading to _HAS_CXX20 being 0).
The problem in my case was that the .vcxproj file had a language standard 'condition' still being stdcpp17 (while the language standard was correctly at stdcpp20). Removing these lines from the file worked.
See: https://developercommunity.visualstudio.com/t/Visual-studio-wont-compile-with-the-c/10378148

zerocukor287
  • 555
  • 2
  • 8
  • 23
pklop
  • 21
  • 2
1

Set the platform to "All Platforms" and then make my changes.

Then they flow through the project correctly.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140