C++ enums with additional data (similarly to ML/Haskell datatypes)

Viewed 27

I would like to create a function that returns an enumeration value, but just in case of specific values, it also provides some additional data. In SML I would define the return type as

datatype rettype = A | B | C of data | D of data

... where data is some previously defined type.

In C++, I can do the following:

enum class AuxEnum { A, B, C, D };
using RetType = std::pair<AuxEnum, std::optional<Data>>;

While this works, it bothers me that there is no actual connection between the enumeration value and whether additional data is supplied.

Is there a better option?

0 Answers
Related