Neargye / magic_enum

Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code
MIT License
4.88k stars 434 forks source link

Enum array does not warn if initialized with too few values #303

Closed turol closed 11 months ago

turol commented 1 year ago

This code raises neither warnings nor errors on GCC or clang:

#include <magic_enum_containers.hpp>

enum class Count : uint32_t {
One, Two, Three
};

magic_enum::containers::array<Count, std::string> notEnough = 
{
 "one", "two"
};

int main() {
    for (const auto &p : notEnough) {
        printf("\"%s\"\n", p.c_str());
    }
}

This makes it hard to add new values to enum since the compiler doesn't complain.

Neargye commented 11 months ago

It's just an array by design, with automatically conversion enum -> index


enum class Count : uint32_t {
One, Two, Three
};

std::array<uint32_t, std::string> notEnough = 
{
 "one", "two"
};
notEnough[(uint32_t)Count::One] = ....;