c++ - 如何在 Visual C++ 2019 社区中使用 c++20 std::is_constant_evaluated?

标签 c++ visual-c++ std c++20

MS 声称在其 conformance page 上支持 is_constant_evaluated并且它们的 header 确实包含一个实现,由 #if _HAS_CXX20 保护。

似乎(至少在 MSVC Community 16.6.5 中)/std:c++latest 不会导致定义 _HAS_CXX20。 UI 不提供/std:c++20 选项。

例如

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

给出以下输出:

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 ==========

请注意,使用 is_constant_evaluated 会产生错误,而 static_assert 则不会。

我错过了什么?

最佳答案

它对我来说工作正常,_HAS_CXX20 设置正确。并且 is_constant_evaluated 按预期工作。

std::cout << _HAS_CXX20;

输出1

我的实现:

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

问题似乎是您使用的是 #ifdef 而不是 #if

关于c++ - 如何在 Visual C++ 2019 社区中使用 c++20 std::is_constant_evaluated?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63284423/

相关文章:

c++ - C++编程入门

C++ lib查看器下载

visual-c++ - wglCreateContext() 每次在 OpenGL 和 Visual C++ 中返回 NULL

C++11:在不知道其容器的情况下存储迭代器

linux - 在结构列表上调用 std::sort 时将 const 作为 'this' 参数错误传递

c++ - 无法将派生比较传递给 std::priority_queue

c++ - 优先队列中的池内存分配

C++11:模板参数重新定义默认参数

c++ - 如何查找是否以及不带括号的语句?

c++ - PostMessage 不会触发 Message Handler