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

Kyle Edwards kyle.edwards at kitware.com
Tue Oct 11 21:25:13 CEST 2022


On 10/10/22 05:55, Volker Hilsheimer via Development wrote:
> Hi,
>
>
> We are using `#pragma once` in a number of examples and tests in the Qt source tree, but I don’t think we have officially endorsed it in favour of explicit include guards.
>
> #pragma once is “non-standard but widely supported” [1], with some caveats, e.g. when there are multiple header files with the same name (which each compiler then handles differently, as there is no standard).
>
>  From what I see, it should in practice be ok to use. But perhaps I’m missing something. Does anything speak against using ‘#pragma once’ in new files?

Speaking as co-maintainer of CMake, we have effectively required #pragma 
once to build CMake itself since August 2017, we officially codified 
this as policy in September 2020, and we will soon be writing a 
clang-tidy plugin to enforce this in our CI. We have not received any 
complaints about it. Just my $0.02.

Kyle



More information about the Development mailing list