neither include guard nor pragma is a standard solution.
This means that we either rely on a bug prone hack or non standard solution to obtain the effect of:
“Include / import the file only once.”
Which seems like it’s a missing feature,
it’s such a common use case that it should be standardized.
Also not changing other file but stating:
#include once “header.hpp”
Is preferred in my opinion since you might not own the other file maybe it’s a part of some other repo.
P.S.
Maybe worth mentioning is the fact that I never want to include a file more than once.
P.S.S.
Should we not standardize include once since we have a non standard option available everywhere?
Shouldn’t we standardize everything that’s useful and thus lower the chance for bugs?
Cheers, Filip
Wiadomość napisana przez Bo Persson via Std-Proposals <std-proposals@lists.isocpp.org> w dniu 24 mar 2025, o godz. 08:11:
On 2025-03-24 at 02:51, Muhammad via Std-Proposals wrote:Last thing about #include once, As with all preprocessing directive it get affected with the conditional #if family of preprocessing directive, for example:
//d.hpp
#ifdef D_HPP
# include once
#endif
The file d.hpp will get included every time it appears in the #include until the macro D_HPP gets defined.
What if D_HPP gets undefined again in another header? There are tiny corner cases all around!You also have the problem of how you find the header:"A preprocessing directive of the form # include < h-char-sequence > new-linesearches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined."(https://eel.is/c++draft/cpp.include#2)There are two "implementation-defined" right here.Later it continues:"The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined.The implementation shall provide unique mappings for sequences consisting of one or more nondigits or digits ([lex.name]) followed by a period (.) and a single nondigit. The first character shall not be a digit. The implementation may ignore distinctions of alphabetical case."So d.HPP and D.hpp might be different files? Or not?I have worked on mainframes (IBM z/OS) where the file system doesn't have directories or extensions on file names. Another corner case (where d.hpp might be HPP(D) on disk).-- Std-Proposals mailing listStd-Proposals@lists.isocpp.orghttps://lists.isocpp.org/mailman/listinfo.cgi/std-proposals