C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Get UUID at compile-time from compiler (sort of like __COUNTER__)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 24 Nov 2023 12:29:45 +0000
On Thu, Nov 23, 2023 at 5:13 PM Thiago Macieira wrote:
>
> You can produce reproducible and non-random outputs with __COUNTER__. There
> are pitfalls across TUs, but each TU is reproducible on its own.


I say UUID as slang for a random 128-Bit number, as I think a lot of
people do nowadays. Nobody cares about the different versions of UUID
anymore. When we want a universally unique ID, we just want a random
128-Bit number. Nobody wants your MAC address tagged on to the date
and time.

But here's another idea:

Let's say that __UUID__ is a 128-Bit number as follows:
    * The 96 most significant bits are the same as the 96
       least significant bits of the hash sum of the
       current source file (after all instances of '\r\n' have
       been replaced with '\n') combined with the hash of all
       the preprocessor macroes defined at the command line
       (for example "-DNDEBUG -D__WXMAC__ -D_FILE_OFFSET_BITS=64").
    * The remaining 32 least significant bits are __COUNTER__

This would mean we have reproducible builds.

Not sure what we would do about having identical source code files
(i.e. with the same hash sum) or even if that would make any sense --
I mean in order to avoid the one-definition rule, all functions would
have to be static-linkage or inside an anonymous namespace, meaning
they would be totally inaccessible outside the translation unit,
unless of course there were _one_ external linkage function providing
access to them -- but then that would violate the ODR.

Received on 2023-11-24 12:29:58