22

- Background Information:

There is a class in C++11 known as enum class which you can store variables inside. However, I have only seen the type of the class to be char:

enum class : char {
   v1 = 'x', v2 = 'y'
};

- Question:

Is there any way I can express this enum class of type string?

For example,

enum class : string{
  v1 = "x", v2 = "y"
};

- What I think:

I tried using it, but I got errors, I am not sure if I am doing it right or not. The reason why I want to use strings is that they are capable of holding multiple characters at the same time, so it makes them more useful for my code.

2
  • 1
    @CinCout Everything in your comment is incorrect. Underlying type of a scoped enum is int only if you haven't specified a type, which is not the case in the example above. And char is certainly not an int. Commented Apr 20, 2017 at 5:17
  • 2
    @Kourosh There is a class in C++11 known as enum class ... no, there's no such class, enum class is used for defining scoped enumerations. The answers below link to documentation that explains what those are. Commented Apr 20, 2017 at 5:19

3 Answers 3

12

There is no way to do that in C++11 or C++14. However, you should consider having some enum class, then code some explicit functions or operators to convert it from and to std::string-s.

There is a class in C++11 known as enum class which you can store variables inside.

That phrasing is not correct: an enum class does not store variables (but enumerators).

So you might code:

enum class MyEnum : char {
   v1 = 'x', v2 = 'y'
};

(this is possible, as answered by druckermanly, because char is an integral type; of course you cannot use strings instead)

then define some MyEnum string_to_MyEnum(const std::string&); function (it probably would throw some exception if the argument is an unexpected string) and another std::string MyEnum_to_string(MyEnum); one. You might even consider also having some cast operator calling them (but I don't find that readable, in your case). You could also define a class MyEnumValue containing one single data member of MyEnum type and have that class having cast operator, e.g.

 class MyEnumValue {
    const MyEnum en;
 public:
    MyEnumValue(MyEnum e) : en(e) {};
    MyEnumValue(const std::string&s)
     : MyEnumValue(string_to_MyEnum(s)) {};
    operator std::string () const { return MyEnum_to_string(en);};
    operator MyEnum () const { return en };
    //// etc....
 };

With appropriate more things in MyEnumValue (see the rule of five) you might almost always use MyEnumValue instead of MyEnum (which perhaps might even be internal to class MyEnumValue)

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I see, maybe It would be better to say hold variables ?
No, better use the standard terminology of enumerators.
6

No, this is not possible.

http://en.cppreference.com/w/cpp/language/enum states:

The values of the constants are values of an integral type known as the underlying type of the enumeration.

The key point being "integral type" -- a string is not an integral type.

Comments

1

Compiler internally converts your char to its equivalent int representation (ASCII). So it would not be possible to use string instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.