I know that the following typedef-based code works fine and is widely used:
class Example {
public:
typedef enum {
eOrdered,
eRandom
} tePoolOrder;
};
Clang-tidy has now automatically modernized it as follows.
class Example {
public:
using tePoolOrder = enum {
eOrdered,
eRandom
};
};
This compiles without issues in my C++17-setup (Clang/GCC), but I’m unsure whether this syntax is standard-compliant or if it relies on compiler-specific behavior.
I understand that using typically aliases named types, so is this usage with an anonymous enum supported by the C++ standard? If not, should I switch to a named enum for portability? What would be the correct modern C++ approach to achieve this?
Any clarification would be appreciated!
Try it out here: https://onlinegdb.com/3FuchkJ5Y
Simplified, the syntax of using
for type-aliases is
using alias = type;
For your case the alias is tePoolOrder
and the type is enum { ... }
.
This is perfectly valid.
This is very similar to typedef
, which has the syntax (again simplified):
typedef type alias;
Hopefully this helps to see that the type is the same, and that enum { ... }
is a type and not an anonymous enumeration.