On 28 June 2012 17:00, Rick Hodgin wrote:
> I'd like to add an inverse definition to an existing BOOL/bool type, one
> which the compiler is natively aware of.
>
> Example:
> bool isSystemOpen;
>
> I can reference this in the manner in which it's defined:
> if (isSystemOpen)
> if (!isSystemOpen)
>
> However, there are times when it's more desirable to reference it in the
> opposite manner as it makes more logical sense to humans.
>
> The compiler is aware of the boolean nature of that variable, so I would like
> to be able to create a new form which is aware of the inverse boolean
> condition natively.
>
> Example syntax:
> bool isSystemOpen[.isSystemClosed.];
>
> This new syntax creates one physical variable, and two logical ways to
> reference the same variable in memory, but always tests for the inverse
> condition correctly, such as:
> if (isSystemClosed)
>
> Which would be the same logically as:
> if (!isSystemOpen)
>
> Any ideas on how to best / easily implement this? :-) I had the idea of just
> writing a pre-processor to look for those references and swap them out with
> the opposite condition. But it seems GCC should be able to do this natively.
Why do you want to bother with a non-standard, unportable extension
instead of just writing:
inline bool isSystemClosed()
{ return !isSystemOpen; }
Which is simple, conventional, easy to understand and portable.
Or in C++ just define a suitable type, instead of needing changes to
the core language:
struct inv_bool {
bool& b;
operator bool() const { return !b; }
};
inv_bool isSystemClosed = { isSystemOpen };