【问题标题】:How to import C assert in cpp20 experimental如何在 cpp20 实验中导入 C 断言
【发布时间】:2020-03-28 10:33:38
【问题描述】:

我想在 Visual Studio 2019 中将 C assert 与 C++20 一起使用(使用 std:c++latest 编译),但我找不到要导入的正确模块。是否已经支持,如果支持,我应该导入哪个模块?

import std.core;
constexpr auto f()
{
    return "Hello world!";
}
int main()
{
    static_assert(f() == "Hello world!"); // Compile time assert works.
    //assert(f() == "Hello world!"); // How to import runtime assert()?
    //[[assert: f() == "Hello world!"]] // Contracts were removed from cpp20.
    std::cout << f();
}

输出:世界你好!

【问题讨论】:

    标签: c++ visual-studio-2019 c++20 c++-modules


    【解决方案1】:

    模块不包含宏。导入旧版模块头文件会引入宏,但 import std.core; 或其他任何东西都不会引入宏。

    【讨论】:

      【解决方案2】:

      模块可以与头文件一起使用。 C++ 源文件可以导入模块以及#include 头文件。我建议你可以尝试添加#include &lt;assert.h&gt;

      【讨论】:

      • 有效,但要小心:这在序列中首先包含标头,然后导入模块#include &lt;assert.h&gt; \n import std.core;。反过来,它会导致编译器错误,因为内部的宏例如 corecrt.h 没有被预处理器评估(至少目前在 VisualStudio 中)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      • 2022-06-28
      • 2022-12-04
      • 1970-01-01
      • 2019-06-04
      • 2010-10-11
      • 2014-12-03
      相关资源
      最近更新 更多