How do I use c++20 std::is_constant_evaluated in Visual C++ 2019 Community?

Viewed 914

MS claim to support is_consant_evaluated on their conformance page and their <type_traits> header does include an implementation, guarded with #if _HAS_CXX20.

It seems though that (with MSVC Community 16.6.5, at least) /std:c++latest does not cause _HAS_CXX20 to be defined. The UI does not offer a /std:c++20 option.

e.g.

    #include <type_traits>
    static_assert( std::is_constant_evaluated(), "" );
    #ifdef _HAS_CXX20
    static_assert(false, "");
    #endif

gives the following output:

1>ConsoleApplication7.cpp
1>ConsoleApplication7.cpp(3,21): error C2039: 'is_constant_evaluated': is not a member of 'std'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\type_traits(20): message : see declaration of 'std'
1>ConsoleApplication7.cpp(3,42): error C3861: 'is_constant_evaluated': identifier not found
1>ConsoleApplication7.cpp(5,15): error C2338:
1>Done building project "ConsoleApplication7.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Note that the use of is_constant_evauluated produces an error, and that the static_assert does not.

What am I missing?

1 Answers

I can't reproduce your errors, it works fine for me, _HAS_CXX20 is properly set. and is_constant_evaluated works fine.

std::cout << _HAS_CXX20;

outputs 1.

My implementation:

#if _HAS_CXX20
// FUNCTION is_constant_evaluated
_NODISCARD constexpr bool is_constant_evaluated() noexcept {
    return __builtin_is_constant_evaluated();
}
#endif // _HAS_CXX20

My version details:

enter image description here

Hope it helps.

Anyway, if __builtin_is_constant_evaluated() works, just define _HAS_CXX20 as 1, problem solved.

Related