C++ Logo

std-proposals

Advanced search

Re: [std-proposals] #pragma once safer alternative

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 24 Mar 2025 12:12:57 +0100
pon., 24 mar 2025 o 11:20 Filip via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> My 2 cents here would be that like you mentioned before:
> 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?

Or maybe we move away from includes in the first place?
As if we even standarazie "once", when will you be able to use it in
your project?
Even simple change will take at least 6y to propagate and probably in
this time frame
C++ modules will have reasonable support in the majority of
environments (as it will be then 11y old).
At lest current trunk of GCC and Clang compile simple module code:

https://godbolt.org/z/9W19enz1n

MSVC get timeout, but this mean it try compile it too. Couple of years
more and it could be production ready.

>
> Cheers, Filip
>
> Wiadomość napisana przez Bo Persson via Std-Proposals <std-proposals_at_[hidden]> 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-line
>
> searches 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 list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-03-24 11:13:10