Hacker News new | ask | show | jobs
by kazinator 2760 days ago
If you want to make #pragma once robust, you firstly have to specify the requirements rigorously. The requirements can be specified in such a way that checksumming of files is avoided. We can stipulate that exact (as well as inexact) copies of a file are distinct objects with their own identity under #pragma once, and are not mutually excluded.

#pragma once can be defined in terms of a reference model whereby it is equivalent to a machine-generated sequence:

  #ifndef <ident>
  #define <ident>

  #endif
where the detailed semantics is tied to how the machine generates <ident>.

If <ident> is a digest of the absolute path, then references to the same file via different hard or symbolic links look different and do not mutually exclude.

If <ident> is produced from the volume and object identifier (like inode number) then different links to the same header will mutually exclude.

If <ident> is a content hash, then identical files will mutually exclude (but we need to deal with hash collisions somehow).

A much better solution would be to sidestep this whole thing entirely and just allow any file-scope definition in C++ to be repeated in the same translation unit (with some proviso, like that the multiple definitions have to be identical; and that could be enforced with diagnostics). The multiple inclusions of the same material aren't a problem.