Highest Rated Comments


Enemii3 karma

C++11 introduces strongly typed enums, a preferable and safer alternative in most cases to unscoped enums. Unfortunately providing operators for strongly typed enums is tedious, and conversion operators cannot be defined. If more control over the provided operators was allowed, it would allow strongly typed enums not just to be used as a way of enumerating value's, but instead as a strongly typed integral. It improves the usability, allowing for bitmasks, units, and any other abstraction to preform some subset of operations safely.

Essentially it would function similarly to the proposed opaque typedefs, except strictly on integral types. Obviously there are benefits to being allowed to represent values as strongly typed integral constants. It can be used as a template overload as both a value parameter and as a type parameter (std::integral_type) and forward declared enums are complete types.

You would by default get the same type safety guarantees as strongly typed enumerations except for where you explicitly specify otherwise. Since the compiler knows exactly how to preform any of the default operators on a strongly typed enum, why not just ask it? I propose an extension that allows operators to defaulted.

For example:

enum class A
{
    zero,
    one,

    // Tell compiler to use the built-in operator
    A operator+=(A) = default; 

    // Option 2: Less ambious to parse, makes it clear that this is not a member function
    // But could conflict with an external definition
    friend A operator+=(A, A) = default;

    // enumerator definitions can be mixed in
    two
};

note: operators cannot be declared, or defined unless by default

It believe that the modification to the grammar would be reasonable. Especially if parser look-ahead is not a concern. but am entirely ignorant to the realities of implementing this.

Along with the unary and binary arithmetic and bitwise operators, other operators should be considered as well; including increment and decrements, as well as operator bool and operator underlying_type.

If it is possible to use the built-in operator semantics, that would be preferable, It would be even better if you could default an arbitrary integral type on the right hand side.

While I think this alone would be a huge step forward, allowing for users to declare and define friend functions within the enum class would be very useful as well. While we are at it, why not typedefs and string conversions!

Enemii2 karma

Thanks for the response. What advice would you give to somebody who wants to become involved in C++ language evolution? ( also do you know what paper contains the proposed changes for C++ 14. I haven't been able to find it )