[Development] #pragma once

Konstantin Tokarev annulen at yandex.ru
Wed Jan 24 12:53:18 CET 2018



24.01.2018, 14:33, "Jean-Michaël Celerier" <jeanmichael.celerier at gmail.com>:
> I certainly have been bitten much more times by include guards that were the same in different files (especially in old libraries where guards look like #ifdef QUEUE_H because of course there is a single queue.h file in the whole world, or because someone just copy-pasted the content of a file to another and forgot changing include guards -- I plead guilty!) than whatever hypothetic bug in pragma once GCC's implementation may have.
>
> Are there people here who had, just once, a bug due to #pragma once ? I never met any. Across network drives, with precompiled headers, unity builds, with MSVC, Clang, GCC, ccache, distcc and Icecream...

It is possible to get beaten by double inclusion if any headers are copied somewhere
in the build process. We've stumbled upon this in WebKit project, where Windows-based
ports are copying headers instead of merely creating forwarding ones (because of
specifics of Apple's internal build process). Compiler believes that different files are
different headers, so happily includes both when it gets a chance [1]

That's said, if headers are never copied it's safer to use #pragma once than include guards

[1] http://mac-os-forge.2317878.n4.nabble.com/unified-sources-build-forwarding-headers-that-are-copies-td348352.html

>
> -------
> Jean-Michaël Celerier
> http://www.jcelerier.name
>
> On Wed, Jan 24, 2018 at 11:34 AM, Mitch Curtis <mitch.curtis at qt.io> wrote:
>
>>> -----Original Message-----
>>> From: Ville Voutilainen [mailto:ville.voutilainen at gmail.com]
>>> Sent: Wednesday, 24 January 2018 11:25 AM
>>> To: Alexander Nassian <nassian at bitshift-dynamics.com>
>>> Cc: Mitch Curtis <mitch.curtis at qt.io>; development at qt-project.org
>>> Subject: Re: [Development] #pragma once
>>>
>>> On 24 January 2018 at 12:22, Alexander Nassian <nassian at bitshift-
>>> dynamics.com> wrote:
>>> > Maybe because it’s not part of the C++ standard?
>>>
>>> #pragma once is not a replacement for include guards.
>>
>> Why not?
>>
>>> It's not part of the C++ standard because it doesn't always work
>>
>> In which ways? My quick search gave me these:
>>
>> https://stackoverflow.com/a/1946730/904422
>> https://en.wikipedia.org/wiki/Pragma_once#Caveats
>>
>> There's also this answer that highly recommends against it, but seems quite contended in the comments:
>>
>> https://stackoverflow.com/a/34884735/904422
>>
>>> and modules are a superior solution anyway.
>>
>> How so?
>> _______________________________________________
>> Development mailing list
>> Development at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/development
> ,
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development


-- 
Regards,
Konstantin



More information about the Development mailing list