-
-
Notifications
You must be signed in to change notification settings - Fork 650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forward-declaring identifiers in std:: is UB - consider including some of the cheaper C/C++ stdlib headers #194
Comments
If you work on linux, consider the following 2 source files. // a.cpp
int main()
{
} //b.cpp
#include <iosfwd>
#include <cstring>
#include <cstddef>
int main()
{
} Now run some timings with time g++ a.cpp
time g++ b.cpp You will notice at most 20ms difference, usually 10ms. On the other hand, if you include |
You are absolutely correct about I've thought about this aaand I'm still not considering including them - that is until that UB actually manifests itself in some problematic way. This is perhaps the "safest" type of UB and I know anyone pedantic about the standard won't like this choice... But that is what I have that huge build matrix on the CI! And for the iostream stuff - I could include https://slides.com/onqtam/2017_cppcon_doctest#/49 (the note about boost.DI from that slide is no longer true...) For doctest 3.0 I might move to modules and import the symbols I need. I'm keeping this issue open just for reference. |
This is a showstopper issue for me. Lets say that some implementation decides to use inline namespace inside namespace std with some update. Bam, the code stops compiling. |
I don't this is a problem. cstdio is cheap too. All C headers are, because they are just a bunch of declarations. |
Inline namespaces - that is actually the case with libc++ on clang - so when clang is used I first include
So far I assumed that when such an issue arises with libstdc++ or the MSVC stl I could switch to the proper headers being included and just push a new version of doctest. So far it seems that GCC 9 and VS 2019 won't cause such trouble. But on the other side - users would feel annoyed if after updating their toolchains they end up having to update their third party dependencies as well... I'll think about this a bit more. EDIT: It is really appealing to have 1 thing less to worry about - I might play with this in a few days and do the benchmarks with a few includes |
I have provided the patch, merge it if you want. Correctness is more important than speed. I'll mention again, real world use cases don't lose a millisecond, only the synthetic benchmark. |
This is not going to happen due to ABI break. For implementation that has used inline namespace already, we can still predeclare these names by looking at which implementation this is: a36f6b8 |
I just closed the PR related to this and the reasoning is there in the final comment: #197 (comment) |
I noticed that you redefine some stuff that is already part of C and C++ standard library.
For example:
memcpy
insidemy_memcpy
nullptr_t
(which is UB)cstddef
andcstring
are two headers that are EXTREMELY common. I can't think of a single non-trivial program that does not already includes these two headers. Just including C++ string or vector brings in these two. So, if some client code has already included them, and Doctest includes them, the second inclusion is practically a no-op because of header include guards.Therefore, when redefining this stuff, you are optimizing the compile time for the best case, and that is using doctest alone, but you are worsening the common case, which is using doctest to test some other software.
The text was updated successfully, but these errors were encountered: