C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Mon, 24 Mar 2025 21:33:46 +0100
Hi Marcin,   import your_library; #include "your_library_imported.h" #include "third_party/A.h" // #include "your_library/a_header.h" but got skipped   Wouldn't it be good, if import automatically (and optionally) includes a header file specified by the library module? Or it would not work as #include is for the preprocessor, whereas import is not?   -----Ursprüngliche Nachricht----- Von:Marcin Jaczewski via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mo 24.03.2025 21:11 Betreff:Re: [std-proposals] #pragma once safer alternative An:std-proposals_at_[hidden]; CC:Marcin Jaczewski <marcinjaczewski86_at_[hidden]>; Bo Persson <bo_at_[hidden]>; pon., 24 mar 2025 o 14:20 Henry Miller via Std-Proposals <std-proposals_at_[hidden]> napisał(a): > > > > On Mon, Mar 24, 2025, at 05:19, Filip via Std-Proposals wrote: > > 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? > > What does it mean for a file to be the same?  This ends up harder than you would expect and essentially forces any and thus forces the obvious answers to come with the strict warning "If you are writing a library for use by others you may not use this"! > > There are two options to check if a file is identical.  The first is it is the same thing on the filesystem - in unix follow all the symbolic links and then check the inode of the final file, Windows have similar concepts though different names, I'm not sure about other operating system.  The second is do some form of binary compare on the file. > > The first fails because a number of us - for reasons we don't want to talk about - have installed a library twice into two different paths and that same file can be found by both paths in one translation unit. > > The second fails because a common pattern used by many projects is >     #widget.h >     #include "impl/widget.h" > Note that these files will be binary identical despite the files they ultimately include being very different. > > Because of the above any attempt to standardize #pragma once must come with the warning that if you are writing a third party library you are not allowed to use it.  The standard currently doesn't really have any way to write the above, but it is a sticking point because if you use it your customers will have the problems not you and that means it is very painful for both sides when issues are encountered. > > Now "#includeonce UniqueString" avoid the above problems. I'm not sure what the objections to that are.  If you want to propose this I can add an additional motivation: I once worked on a project where the #endif for the include guard was in a different file from the #ifndef/#define - and the whole project really was a mess to understand because of such tricks. > > Still in the end I agree with those who are suggesting modules are the answer.  They are still in early adopter stage and so might have some other limitation currently unknown, but they solve a number of problems and so seem like the best path forward anyway.  You need to convince people that despite modules this is still a path worth working on. Inspired this discussion and looking on current C++ module status I find this: https://clang.llvm.org/docs/StandardCPlusPlusModules.html#providing-a-header-to-skip-parsing-redundant-headers Interesting case where you have duplication of headers with modules that slow down compilation. And proposed solution is: ``` #pragma once #ifndef YOUR_LIBRARY_IMPORTED ... #endif ``` Add include guard on top of `#pragma once` :D This means in the long run you need `#ifndef` if you want to have more interactions with modules anyway. Then this add another layer of problems for `#pragma once`: How it can detect if declarations are already present even if NO header file was loaded yet. > -- > 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 20:39:16