C++ Logo

std-proposals

Advanced search

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

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Sat, 22 Mar 2025 23:30:10 -0500
Hi,

The problem with pragma once is that every implementation has a *slightly*
different way of determining file uniqueness (whether by file path, file
contents, modification time, inode, etc). These each have their pros and
cons but the implementation divergence is where the portability problems
originate. Your proposal doesn't solve these problems. If you are
interested, here's a 200-email long chain from when I tried to write a
paper about it in the past
https://lists.isocpp.org/std-proposals/2024/08/10949.php.

I proposed it with the same #pragma once spelling because I was trying to
standardize existing practices. But, inevitably, this would require some
implementations to change their behavior slightly. Additionally, it seems
hard to convince people this is worth solving since modules are finally
here for real this time. (half kidding, hopefully but we'll see).

Alternative spellings, like just #once, have been proposed before to both
WG21 and WG14 without success:
- https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2896.htm
- https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0538r0.html

Cheers,
Jeremy

On Sat, Mar 22, 2025 at 11:16 PM Muhammad via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> The #include syntax (from cppreference.com)
>
> 1. # include < h-char-sequence > new-line
> 2. # include " h-char-sequence " new-line
> 3. # include pp-tokens new-line
>
> The pp-tokens in (3) is a macro that get expanded until it matches (1) or
> (2), and when it doesn't it is undefined behavior.
>
> This proposal is to add another syntax for #include
>
>
> 1. # include < h-char-sequence > new-line
> 2. # include " h-char-sequence " new-line
> 3. # include once new-line
> 4. # include pp-tokens new-line
>
>
> The fact that by convention all macros are written in all uppercase pr
> starts with underscore make this proposal very unlikely to break existing
> code.
>
> The implementation for this proposal is already done using the
> compiler-specific #pragma once syntax but here it will be more convenient
> and a standard way to not include it more than once.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-03-23 04:30:25