It is not true that the compiler will optimize this, but it avoids UB provided that the supplied value is indeed a float representation (that is, its right size and its bitmap do not contain a trap float representation). GCC, at least sometimes, can optimize it:
float convert(int32_t inputvalue) { float f; std::memcpy(&f, &inputvalue, sizeof(f)); return f; }
If optimization is an important part of the question, then the official answer is that it is not possible to guarantee that an unknown compiler will perform the given optimization. But this is harder to optimize than most. It relies on a compiler to βunderstandβ what memcpy does, which is more in demand than to βunderstandβ what a pointer listing does.
source share