Equivalent of "using namespace X" for scoped enumerations?
Solution 1:
So, is there a way to avoid having to type
CatState::
all the time?
Not before C++20. Just as there's no equivalent for having to type ClassName::
for static class members. You can't say using typename ClassName
and then get at the internals. The same goes for strongly typed enum
s.
C++20 adds the using enum X
syntax, which does what it looks like.
You can of course not use enum class
syntax, just using regular enum
s. But then you lose strong typing.
It should be noted that one of the reasons for using ALL_CAPS for weakly typed enums was to avoid name conflicts. Once we have full scoping and strong typing, the name of an enum is uniquely identified and cannot conflict with other names. Being able to bring those names into namespace scope would reintroduce this problem. So you would likely want to use ALL_CAPS again to help disambiguate the names.
Solution 2:
So the short answer is no, but fortunately this is going to change in a recently finalized feature set of C++20. According to this accepted proposal you will be able to do the following:
enum class CatState
{
sleeping,
napping,
resting
};
std::string getPurr(CatState state)
{
switch (state)
{
using enum CatState;
// our states are accessible without the scope operator from now on
case sleeping: return {}; // instead of "case CatState::sleeping:"
case napping: return "purr";
case resting: return "purrrrrr";
}
}
Solution 3:
You might consider using a typedef
to shorten the qualified names:
typedef CatState C;
Or, if the columns are repetitive in a way that they can be generated easily, you might consider using a macro to generate each row in the table, which can lead to very concise (and easier to read) code.
Solution 4:
Nicol's answer is correct: the language is designed to make you always qualify scoped enumerators (except in the enum { }
scope itself).
However, here is a technique I came up with for "scoped" enumerators that are unscoped within chosen classes. Technically, the enumerators are unscoped, so they will still convert implicitly to int
. (Not "strongly-typed" as you put it.) Nevertheless, in the idiom they are accessed using the scope operator after a true enum
name, so syntactically there isn't a difference — and therefore it requires C++11.
#define IMPORTABLE_ENUM( TYPENAME, ... ) \
\
struct import_ ## TYPENAME { \
enum TYPENAME { \
__VA_ARGS__ \
}; \
}; \
\
typedef import_ ## TYPENAME :: TYPENAME TYPENAME;
// usage:
IMPORTABLE_ENUM ( duck, huey, dewey, louie )
duck d = duck::dewey; // can't use unscoped enumerators here
struct duck_madness : private import_duck { // but inside a derived class
duck who_did_it() { return huey; } // qualification is unnecessary
};