error: lvalue required as unary & operand

The address-operator & requires a variable to take the address from. The result of your cast (long)u32_Time is a temporary that does not necessarily reside in memory and therefore has no address that could be taken. So if that piece of code ever compiled somewhere it was a nonstandard compiler extension.

The standard, §5.3.1,3 demands:

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue […]

How to fix this: std::localtime expects a pointer to a std::time_t so you best provide that. You did not provide any explanation or further code, so I can only guess that u32_Time is some 4 byte unsigned arithmetic type that is supposed to represent a time in some manner. How that is properly converted into a std::time_t depends on how your compiler implements the latter and how you got the value of the further. Simply applying a C-cast is not portable, and casting to long is even less portable.
If, and only if the std::time_t on your current platform is also a unsigned 32 bit type using the same representation as your u32_Time, it might suffice to use

 localtime(reinterpret_cast<std::time_t*>(&u32_Time));

More portable would be storing the value in the correct data type first:

 std::time_t time = u32_Time;
 localtime(&time);

That way you will get the necessary warnings and/or errors if time_t and the type of u32_Time are not compatible.

I would strongly advice against using C-casts, because once you have to port this piece of code to another platform you will have no means to find that nasty cast easily.

Leave a Comment