C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Mon, 10 Mar 2025 17:56:13 +0100
Header files can include themselves and be included repeatedly by the same translation unit. You could - if you want to go in that direction - also take the translation unit after all headers where included. That is a certain stage of preprocessing. Then the headers just belong to the translation unit itself. Of course that would not be practical, if you want to get the same UUID from the same header within different translation units. Then you could just take the translation unit up to the _UUID macro. But then the order of header included would make a difference.   -----Ursprüngliche Nachricht----- Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Mo 10.03.2025 16:38 Betreff:Re: [std-proposals] Random numbers in identical builds An:std-proposals_at_[hidden]; CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>; People want random numbers at compile time that stay the same if they hit "Rebuild" but which are still sufficiently random. Even though not every C++ implementation is guaranteed to have a 128-Bit integer type, I'm still going to go with 128-Bit random numbers, which I refer to loosely as a "UUID" because there is absolutely no merit in monkeying about with the so-called "different versions" of UUID. Nobody cares about the current time or your network card's MAC address. We just want a random number, so I use the term UUID to mean a 128-Bit random number. So there's two ways of doing this: (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(); 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) 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"? -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2025-03-10 17:01:15