[Development] Using #pragma once

Thiago Macieira thiago.macieira at intel.com
Sun Oct 7 20:39:22 CEST 2018

On Sunday, 7 October 2018 01:56:47 PDT Lars Knoll wrote:
> Hi,
> Just a quick question: Does anybody have any good arguments against us
> starting to use #pragma once instead of header guards throughout our code
> base?

Yes, two:

a) not supported everywhere
b) not well-defined behaviour when it comes to anything but the simplest 

For example, I have ~/src as a bind-mount to ~/dev/src. That means 
are actually the same file, but they aren't for the effects of #pragma once 
because the paths differ.

Another problem is qcompilerdetection.h, qprocessordetection.h, 
qsystemdetection.h, qtypeinfo.h, etc. which depend on the header guards to 
break the include cycle. Ditto for qlist.h + qstringlist.h and 

> I’ve started using it implicitly when updating 3rd party code (the macro
> assembler) in qtdeclarative without any problems (so I’d supported by all
> our compilers). IMO #pragma once is both safer and nicer to use than
> classic header guards.

On all the compilers that compile that macro assembler. We still have 
architectures where they don't.

Another aspect is that the macro assembler headers don't get installed and 
aren't subject to syncqt.pl, so their hierarchy is a lot simpler than for Qt's 
own headers.

I recommend against changing Qt.

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Development mailing list