But to fix the problem properly, we'd have to package the special state as an enum or a bool, together with the float into a struct.
A compromise is to have the float inside a struct, and functions that handle the special value.
I just made a macro to make this easier (not tested, tho):
Code: Select all
#define SPECIAL_VALUE_TYPE( type, name, special_val, meaning ) \
class name \
{ \
type value; \
public: \
name() : value(special_val) {} \
name(type const & v) : value(v) \
{ \
assert( value != special_val ); \
} \
type operator=(type const & v) \
{ \
assert( v != special_val ); \
return (value = v); \
} \
bool meaning() const \
{ return value == special_val; } \
void set_#meaning() { value = special_val; } \
operator type() const \
{ \
assert( value != special_val ); \
return value; \
} \
}
SPECIAL_VALUE_TYPE( float, fuel_in_tank, -1.0f, this_is_a_station_dogbreath );
fuel_in_tank my_fuel;
my_fuel.this_is_a_station_dogbreath() returns true
my_fuel = 77.7f;
my_fuel.this_is_a_station_dogbreath() returns false
float a = my_fuel; works
my_fuel.set_this_is_a_station_dogbreath();
my_fuel.this_is_a_station_dogbreath() returns true
float b = my_fuel; assertion fails