c++ - Visual Studio C++ 2017 模块断言宏

标签 c++ visual-studio-2017

我想利用模块支持在 Visual Studio/C++ 2017 中使用 C++ std 库。我有使用断言宏的代码,但我似乎无法从 std 中获取断言的定义库模块。例如:

import std.core;

void f()
{
    assert(true);    
}

因错误而失败:

1>API_Constants.cpp(10): error C3861: 'assert': identifier not found

我需要做什么来获取断言的定义?

更新 1 感谢推荐的人#include <cassert>那就是我一直在尝试的。不幸的是,对于代码:

import std.core;
#include <cassert>
import std.core;

我收到以下错误:

1>verify-header-compilation.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.12.25827\include\vadefs.h(134): error C2953: '__vcrt_va_list_is_reference': class template has already been defined
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.12.25827\include\vadefs.h(131): note: see declaration of '__vcrt_va_list_is_reference'

注意下面的工作,所以我需要把 #include <cassert>作为任何使用 assert 的 cpp 文件的第一个包含(类似于预编译头包含需要成为第一个包含的方式)。

#include <cassert>
import std.core;
import std.core;

更新 2 虽然适当放置 #include <cassert>解决了我的具体问题 我遇到了与标准 header 中的其他宏相同的问题。特别是我现在遇到了 RAND_MAX 的麻烦来自 cstdlib 并使用相同的技巧是行不通的。似乎这些 header 需要重写,以便可以将包含与导入混合使用,或者我们需要新的 header 来为我们提供宏。

更新 3 注意制作 #include <cassert>由于两个原因,cpp 文件中的最后一个包含是有问题的。首先,我在 header 中使用带有模板的断言,因此我需要编辑大量文件才能使其正常工作。其次,目前无法编译以下内容(失败并出现与上述相同的错误):

import std.core;
#include <cassert>

最佳答案

也许你需要#include <cassert>在文件的顶部。

关于 cpp 模块的 MSVC 文档说:

标准库模块(实验)

std.regex provides the content of header <regex>
std.filesystem provides the content of header <experimental/filesystem>
std.memory provides the content of header <memory>
std.threading provodes the contents of headers <atomic>, <condition_variable>, <future>, <mutex>, <shared_mutex>, <thread>
std.core provides everything else in the C++ Standard Library

所以应该包括在内。但也许 StoryTeller 是对的,宏不包括在内。

关于c++ - Visual Studio C++ 2017 模块断言宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48998441/

相关文章:

npm - 如何在没有 project.json 文件的 Visual Studio 2017 中使用 npm?

c++ - 不稳定的重载?

c++ - boost eval_if 的惰性评估

c++ - 为什么我的 if 语句会出现段错误?

C++删除二进制数组中的前导零

C++,在for循环中分配空间,可能的内存泄漏验证

xamarin.forms - VS2017 是否支持 Xamarin 表单中的“编辑并继续”?

visual-studio-2017 - Nuget 无法恢复 Microsoft.Net.Compilers.1.0.0

oracle - (ORA-12154) 无法从 VS 2017 中迁移的 SSIS 项目连接到 tnsnames.ora 中的数据源

visual-studio - Visual Studio 2017 : Disable Intellisense for CodeSnippets