Is it possible to extend a value type in C++? Alf Steinbach describes how to extend enum values.
Type Safe C++ enum Extensions
by Alf Steinbach
From the article:
Consider if an
enum
like the following,enum class Suit{ spades, hearts, diamonds, clubs };could be extended like
enum class Suit_with_joker extends Suit { joker };where
Suit_with_joker
has all the enumerators ofSuit
plus thejoker
enumerator; and- enumerators introduced in
Suit_with_joker
get integer values following those ofSuit
; and- any
Suit
value is also aSuit_with_joker
value.This would be an example of what I’ll call a value type extension.
The apparently backwards is-a relationship in the last point, where any value of the original type is-a value of the derived type, is characteristic of value type extensions.
C++20 totally lacks support for value type extensions, of
enum
types or other types.
Add a Comment
Comments are closed.