Everything works just fine and dandy. But later on, another developer adds a new fie- I mean member variable to the struct. The following code surprising still compiles because the compiler allows an implicit cast from bool to float. Eww.
What alternative way would you set this up to prevent adding a field from causing a runtime error? I want a compiler error to happen at the place where we attempt to create an outdated version of the struct.
// old code! never updated for "value_extra"
const Foo foo = {
{0.0f},
{true}
};
This would at least give a compile time warning because the conversion of {true} to float is narrowing. I often put the braces around each function argument for added protection.
Maybe try structured bindings? The number of bindings must match the number of non static members of the struct.
Also, try using strong types. They will definitely cause compilation problems if one does the wrong thing, but the flip side is the embuggerance* TM of implementing them in an existing code base.Another advantage of strong types is that one can explicitly control what types can be converted.
* read: nuisance value
I often wish that that the explicit keyword could apply to all functions, not just constructors.
Thanks for the suggestions! I'll try them out. salem c, aggregate initialization might be the simplest thing w/o adding more boilerplate to the struct code itself, and breaking other parts of the code. seeplus, unfortunately making the constructor not implicitly defined breaks some aggregate struct initializations and the compiler isn't fully C++11 compliant, so that's unfortunate -- it appears to work fine in Visual Studio. TheIdeasMan, definitely changing the types to be stronger would produce a compiler error like I wanted, so maybe that's a path forward.