2

Due to my inability to find an answer or the right words to search for I ask here:

I have a (lengthy) class enum defined by

enum class SomeLongName { Foo = 1, Bar = 7, FooBar = 42 };

The only way I know to define a vector of those is:

#include <vector>

enum class SomeLongName { Foo = 1, Bar = 7, FooBar = 42 };

int main() {
  std::vector<SomeLongName> v1 = {
    SomeLongName::Foo,
    SomeLongName::FooBar
  };
  return 0;
}

Is there a way (alternative abbreviated syntax) to use a class enum and don't need to rewrite SomeLongName:: for each value independently? e.g. something like (not working)

#include <vector>

enum class SomeLongName { Foo = 1, Bar = 7, FooBar = 42 };

int main() {
  std::vector<SomeLongName> v1 = (SomeLongName::) { Foo , Bar };
  return 0;
}

In case this matters: I am using MSVC 2019, amd64 (64bit), C++17 on windows 10 64bit

Note: using a typedef like suggested in this stackoverflow discussion thread is not actually what I want or am asking for

2

2 Answers 2

5

C++20 added using enum X syntax, which does what it looks like.

#include <vector>

enum class SomeLongName { Foo = 1, Bar = 7, FooBar = 42 };

int main()
{
    using enum SomeLongName;
    std::vector<SomeLongName> v1 = { Foo, Bar };
    return 0;
}

In previous versions, you can use an enum instead of an enum class.

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

2 Comments

Ah thanks that looks promising. I found it both in the msvc and general c++ supported features per compiler overview being supported by msvc 2019 16.4. But eventhough I have version 16.7 installed I get compiler error (An identifier was expected translated from German) for using enum SomeLongName;, when building via cmake using set(CMAKE_CXX_STANDARD 20) and set(CMAKE_CXX_STANDARD_REQUIRED ON)
You are welcome. I have tried on Visual Studio 16.9.4. Building via CMake using set(CMAKE_CXX_STANDARD 20) passed successfully. I suppose you need to update your VS.
2

Pre C++20: Unfortunately no. Its a "feature" of scoped enums that you have to prepend the name of the enum type. However, before scoped enums were a thing, it was common to wrap enums in a class to avoid polluting the namespace. You can still do that:

#include <vector>

struct SomeLongName {
    enum values { Foo = 1,Bar = 7, FooBar = 42};

    static std::vector<values> make_vect() {
        return {Foo,FooBar};
    }
};

int main() {
    auto v = SomeLongName::make_vect();
}

It's not "nice" but it works.

Past C++20: I refer you to this answer.

3 Comments

This is indeed not very "nice" and actually also more overhead, and worse to read (i think). But still thanks for sharing.
@Martin the "overhead" is only poor style. If thats the only way and you absolutely want it, I wouldnt consider it too bad to sacrifice a bit of style
true, but I don't need it that urgent :)

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.