[Development] Using '#pragma once' instead of include guards?

Eike Ziller Eike.Ziller at qt.io
Fri Oct 14 09:15:00 CEST 2022



> On 13 Oct 2022, at 16:56, Kyle Edwards via Development <development at qt-project.org> wrote:
> 
> On 10/13/22 10:42, Jean-Michaël Celerier wrote:
>> >The only way you’d have a strong case with this is if it has some other significant benefit, like compilation speedup.
>> 
>> The main benefit to me is that it entirely removes possibilities for conflict due to headers having the same name. At least Qt takes great care of avoiding this but still, notice that e.g. the authors of Qt3D's Qt3DCore::QTransform had to be careful to not just do #ifndef QTRANSFORM_H
>> 
>> Now what happens when someone develops a different library but with a header guard similar to Qt's?
>> 
>> If I grep into the various cloned projects on my hard drive, for instance I see
>> 
>> #ifndef QRENDERER_H
>> #ifndef QGLRENDERER_H
>> #ifndef QSEARCHFIELD_H
>> #ifndef QLOG_H
>> #ifndef QJACK_H
>> #ifndef QENC_H
>> #ifndef QRANGESLIDER_H
>> #ifndef QDOUBLERANGESLIDER_H
>> 
>> etc... is the Qt project 100% confident that it will *never ever* use these names? With pragma once this is a 100% non-problem.
> 
> I agree with previous points that while #pragma once can work well for a standalone program, it has the potential to cause problems when used in libraries that other developers use. Even CMake omitted #pragma once from the one (admittedly deprecated) header that may be consumed externally (cmCPluginAPI.h).
> 

> However, there are ways to enforce the use of unique header guards. clang-tidy has an extensible header guard check that can be customized per-project, and plugin loading functionality. Qt could create a clang-tidy plugin that sets up this header guard check and enforces a unique-enough header guard in CI.

That works to avoid clashes inside a project, but doesn't help if user applications mix Qt + their code + other libraries not under their control, which is similar to the issues of #pragma once.
Header guards do have downsides too, but I suppose software developers got used to them and have the necessary workarounds in place. Which is a valid argument for not introducing #pragma once in Qt, which would require different workarounds, and create friction that would need a very compelling argument to inflict.

-- 
Eike Ziller
Principal Software Engineer

The Qt Company GmbH
Erich-Thilo-Straße 10
D-12489 Berlin
eike.ziller at qt.io
http://qt.io
Geschäftsführer: Mika Pälsi,
Juha Varelius, Jouni Lintunen
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B




More information about the Development mailing list