jeudi 9 avril 2020

Are there alternatives to constexpr with character pointer array definitions in C++11 and C++14?

I have some code that I use to give me a list of strings than match the names of enum members so that I can parse text files that use the names easily into the enums. (I actually generate it automatically with a macro but that is a different story.) The code works fine on modern compilers running C++17 but one of my target platforms does not have C++17 support and I'd like to come up with an alternative that works on C++14 (and ideally C++11). The current code is:

class A 
{
    enum MatchType { Linear, Square };
    static constexpr const char * matchTypeStrings[ 2 ] = { "Linear", "Square" };
    static const size_t matchTypeCount = 2;
};

I'd rather not give the enum global scope because that never ends well, and ideally I don't want to add anything to one of the .cpp files (which of course will fix the linker error, but I think it isn't recommended for C++17 code, and won't work with my macro anyway). I was whether I could create a member function that could return the string value instead but I can't think of a neat way of doing it.

Aucun commentaire:

Enregistrer un commentaire