Category : unions

The strict aliasing rule has the purpose of allowing better optimizations. How does this not create problems with unions, where two pointers to two different types can point to the same address? Take this example code (godbolt): #include <cstdint> #include <iostream> void test(uint32_t* ptr32, uint64_t* ptr64) { uint64_t stored = *ptr64; *ptr32 = 0; *ptr64 ..

Read more

Is there a simple way to understand which union member is active? An example: union Read_Value{ char ch; float number; string str; } Suppose a void function reads from an input file stream and initialise Read_Value on the base of the type of the variable read. Assuming I don’t know how the function works, how ..

Read more

While writing unit tests, I got a common declaration error. Error: Undefined reference to ‘class::constructor()’ To remedy this error, I add a forward declaration like so: Before my edit: class::constructor(); After my edit: class::constructor(){} This has always worked well for me prior to today. After I added the forward declaration, I received another error. Error: ..

Read more