T
T
Ternick2020-09-02 19:16:52
C++ / C#
Ternick, 2020-09-02 19:16:52

Generating a random string at compile time?

I figured out for myself a new type of constexpr functions, which is performed at the compilation stage, but there is such a thing that constexpr type functions do not execute functions in their body that have a non-constexpr type.
That is, I can’t call either MessageBox or rand from a function with type constexpr.
On the Internet they write that you need to get a single image of a pseudo-random number based on macros (__TIME__, __TIMESTAMP__, etc.). Is this the case, or can this issue be resolved?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2020-09-02
@Ternick

1. The current timestamp is often taken to initialize a standard C/C++ random number generator. An example can be
found here: https://en.cppreference.com/w/cpp/numeric/random/srand
3. You don't need to basically call MessageBox and any similar functions at the program compilation stage.
constexpr functions are executed by the compiler, so they are quite severely restricted. When the program is executed, instead of calling the constexpr function, there is already a value previously calculated by the compiler.

V
vreitech, 2020-09-02
@fzfx

A compile-time function cannot contain code that is not compile-time capable, because that would prevent the function from executing at compile time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question