Special values - Better Enums (original) (raw)
This is an example of code you can write on top of Better Enums. It's a valid program — you can download it and try it out. The program is also part of the test suite.
Special values
Suppose your project has a convention where each enum has special invalid and_default_ values — for example, Enum::Invalid is invalid, and the first valid constant is default. With Better Enums, you can get the compiler to enforce the convention. At the end of this demo, we will have defined functions and templates that allow us to write:
The compiler will compute default and invalid values automatically, but the programmer will also be able to override the choice. Obviously, the syntax above is very legible and maintainable — the intent is clear and your code base will respond automatically to changes in enum definitions.
Contents
Invalid values
Let's start by defining the invalid values.
#include #include #include <enum.h>
Perhaps the convention is that the invalid value is usually called Invalid, but not for all enums. We will encode that using a template function. The unspecialized version will encode the default policy:
template <_typename Enum_> constexpr Enum invalidimpl() { return Enum::Invalid; }
A macro allows us to override the invalid value by specializing the template:
#define OVERRIDEINVALID(Enum, Value)
template<>
constexpr Enum invalidimpl<_Enum_>() { return Enum::Value; }
Now, we can declare enums like these:
BETTER_ENUM(Channel, int, Red, Green, Blue, Invalid) // Invalid is the invalid value by default
BETTER_ENUM(Compression, int, Undefined, None, Huffman) OVERRIDE_INVALID(Compression, Undefined)
and use them:
static_assert(invalidimpl<_Channel_>() == +Channel::Invalid, ""); static_assert(invalidimpl<_Compression_>() == +Compression::Undefined, "");
This even supports enums that don't have an invalid value at all. As long as they don't have a constant called Invalid, you will get a compile-time error if you try to call invalid_impl<>() on them — as you probably should!
Default values
Perhaps here the convention is the first value that is not invalid is default, unless, again, overridden by the programmer. This can be encoded using only a slightly more complex template function for the general case:
template <_typename Enum_> constexpr _Enum defaultimp_l() { return Enum::size() < 2 ?_ _throw std::logicerror("enum has no valid constants") :_ _Enum::values()[0] == invalidimpl() ? Enum::values()[1] : Enum::values()[0]; }
The above code gives us the first value if it is not invalid, otherwise the second value.
The companion macro for overriding the choice of default value is almost the same as it was for invalid. The difference is that we do an extra sanity check to make sure the programmer doesn't declare the invalid value to be the default. If the sanity check fails, we produce a nice error message. Again, we are assuming that this is dictated by policy.
#define OVERRIDEDEFAULT(Enum, Value)
static_assert(Enum::Value != Enum::Invalid,
#Enum ": default cannot equal invalid");
template<>
constexpr Enum defaultimpl<_Enum_>() { return Enum::Value; }
And, as before, the usage:
static_assert(defaultimpl<_Channel_>() == +Channel::Red, ""); static_assert(defaultimpl<_Compression_>() == +Compression::None, "");
And, if you do
BETTER_ENUM(Answer, int, Yes, No, Invalid) // OVERRIDE_DEFAULT(Answer, Invalid)
you will get a helpful compile-time error sayingAnswer: default cannot equal invalid.
Making the syntax nicer
At this point, our policy is encoded by the ugly-looking functionsinvalid_impl and default_impl. We want a nicer syntax. The main reason we don't just use these functions directly is that the compiler wouldn't infer their template arguments from the context. For example, we would have to write things like
which is unfortunate, because it results in repetition.
In this section, we introduce two global objects called invalid and default_that will implicitly convert to any Better Enum type, and provide the invalid or default value, respectively, when they do so. They will act as new "keywords".
struct invalidt { template <_typename To_> constexpr operator To() const { return invalidimpl(); } };
struct defaultt { template <_typename To_> constexpr operator To() const { return defaultimpl(); } };
constexpr invalidt invalid{}; constexpr defaultt default{};
As you can see, both of these provide the families of implicit conversions that we need. Now, we can test:
static_assert(+Channel::Invalid == invalid, ""); static_assert(+Compression::Undefined == invalid, "");
static_assert(+Channel::Red == default, ""); static_assert(+Compression::None == default, "");
Finally, we can have nice code such as this:
void dump(Channel channel) { std::cout << channel._to_string() << std::endl; }
int main() { dump(invalid);
_Channel channel_ = _default_;
dump(channel);
return 0;}
There are many possible variations of these policies, but I think most of them can be encoded in a reasonable fashion using the tools Better Enums provides. Enjoy!