It is legal in C++ to write:
std::srand(std::time(nullptr));
or does this yield undefined behaviour?
std::time
returns a std::time_t
which is an arithmetic type, but other than that unspecified. From my understanding, arithmetic type is any of the floating point, integer and character types.
std::srand
takes an unsigned int
as a seed value.
Therefore, I would think that you cannot strictly perform this conversion.
I read that on systems conforming to POSIX that std::time_t
is integral and is the number of seconds since 00:00, Jan 1 1970 UTC.
In this case, the conversion can entail converting from signed to unsigned, which is an implementation-defined conversion but should be OK, and from a larger integral to a smaller integral type which is fine too for the seed.