Reputation: 2976
I want to make use of the module support for consuming the C++ std library in Visual Studio/C++ 2017. I have code which makes use of the assert macro but I can't seem to get the definition of assert from the std library modules. For example:
import std.core;
void f()
{
assert(true);
}
Fails with errors:
1>API_Constants.cpp(10): error C3861: 'assert': identifier not found
What do I need to do to get the definition of assert?
Update 1
Thanks to those who suggested #include <cassert>
that was what I have been trying. Unfortunately, for the code:
import std.core;
#include <cassert>
import std.core;
I get the following errors:
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'
Note that the following works so I will need to put the #include <cassert>
as the first include of any cpp file that uses assert (similar to the way pre-compiled header includes need to be the first include).
#include <cassert>
import std.core;
import std.core;
Update 2
While appropriately placed #include <cassert>
resolves my specific question I have hit the same problem with other macros from standard headers. In particular I am now in trouble with RAND_MAX
from cstdlib and pulling the same trick does not work. It would seem that these headers need to be rewritten so that it is possible to mix includes with imports or we need new headers that just give us the macros.
Update 3
Note that making #include <cassert>
the last include in a cpp file is problematic for two reasons. First, I use assert with templates in headers so I would need to edit a non-trivial amount of files to get this to work. Second, the following doesn't compile at present (fails with same errors as above):
import std.core;
#include <cassert>
Upvotes: 1
Views: 4176
Reputation: 902
You need to move #include to be the final header file included, so that the assert macro is not undefined, i.e., so that the assert macro applies to this .cpp file's source code.
Upvotes: 1
Reputation: 1501
Maybe you need #include <cassert>
at the top of your file.
The MSVC doc about cpp modules says:
Standard Library Modules (Experimental)
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
So it should be included. But maybe StoryTeller is right and macros are not included.
Upvotes: 1