C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Random numbers in identical builds

From: Jeremy Rifkin <jeremy_at_[hidden]>
Date: Mon, 10 Mar 2025 13:03:23 -0500
>> (Method No. 2) A constexpr function in the "std" namespace which you
>> would use as follows:
>>
>> constexpr uint128_t myuuid = std::uuid();
>
> Which would be implemented using method No. 1, which means the two are
> effectively the same.

I'm thinking of a super simple implementation that could be done today
that looks like:

constexpr uint64_t compile_time_random(std::source_location location =
std::source_location::current) {
    return random_from_digest(hash(std::format("{}:{}",
location.file_name, location.line)));
}

Alternatively with evaluating differently (but deterministically) at
different points in the file:

constexpr uint64_t compile_time_random(int nonce, std::source_location
location = std::source_location::current) {
    return random_from_digest(hash(std::format("{}:{} {}",
location.file_name, location.line, nonce)));
}

#define compile_time_random() compile_time_random(__COUNTER__)

Cheers,
Jeremy


On Mar 10 2025, at 12:29 pm, Thiago Macieira via Std-Proposals
<std-proposals_at_[hidden]> wrote:

> On Monday, 10 March 2025 08:37:44 Pacific Daylight Time Frederick
> Virchanza
> Gotham via Std-Proposals wrote:
>> (Method No. 1) A preprocessor macro something like "_UUID", which we
>> would use as follows:
>>
>> constexpr uint128_t myuuid = _UUID;
>>
>> which the preprocessor would turn into:
>>
>> constexpr uint128_t myuuid = 11223344556677881122334455667788u;
>>
>> (Method No. 2) A constexpr function in the "std" namespace which you
>> would use as follows:
>>
>> constexpr uint128_t myuuid = std::uuid();
>
> Which would be implemented using method No. 1, which means the two are
> effectively the same.
>
>> Now here's the next thing to address: How do we get the preprocessor /
>> compiler to generate the same UUID every time? Well I think the random
>> number could be generated from six items of data which we can use for
>> this:
>>
>> Datum No. 1: The name of the current source file
>> Datum No. 2: The name of the current header file (if applicable)
>> Datum No. 3: The contents of the current source file
>> Datum No. 4: The contents of the current header file (if applicable)
>> Datum No. 5: The count of how many times _UUID has already been
>> used in the current source file
>> Datum No. 6: The count of how many times _UUID has already been
>> used in the current header file (if applicable)
>
> If the same UUID is desired, then the user shall pass an option to the
> compiler, either in the command-line or as an environment variable,
> which is
> used to seed the random generator (the random generator may be an
> identity to
> the seed in this case).
>
> If the same UUID is not desired, then none of the above are relevant.
> The
> compiler can just read 16 bytes from /dev/urandom and be done with it.
>
>> All six data can be piped into something like the MD5 hash function to
>> give a 128-Bit digest.
>>
>> Plus we would have to discuss what exactly is supposed to happen in a
>> scenario such as the following. Let's say we have a header file as
>> follows:
>>
>> [beginning of header file]
>> constexpr uint128_t myuuid = std::uuid();
>> [end of header file]
>>
>> A const (or constexpr) global variable has internal linkage by
>> default. So if we were to include the above header file in say, 26
>> different source files, then should all 26 source files see the same
>> 128-Bit number? What if we're building on MS-Windows and one file is
>> "myheader.hpp" but gets seen as "MYHEADER.HPP"?
>
> They'd get 26 different numbers. Attempting to determine that the file
> is the
> same or not runs into the #pragma once discussion, which we shall not
> have
> here. In any case, your proposal included the name of the source file
> being
> compiled, which means you *wanted* the output to be different.
>
> More importantly, the problem is under-defined:
>
>> People want random numbers at compile time that stay the same if they
>> hit "Rebuild" but which are still sufficiently random
>
> 'Hit "Rebuild"' is under-defined here. *When* should the number
> change? Is it
> when the file containing the UUID variable is changed? Because in your
> example
> where it is a single line, the file is unlikely to *ever* change,
> which means
> you may get cached build from years ago. But if you did change it,
> even for a
> whitespace change, then everything would rebuild.
>
> Is it when anything in the project changes? Because if I am trying to
> have an
> identifier for my build, I'd want that.
>
> --
> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
> Principal Engineer - Intel DCAI Platform & System Engineering
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2025-03-10 18:03:28