jeudi 4 mai 2023

Can I use constexpr on std:map if needed in a template?

I've a problem using const on a template definition. Here's the code:

typedef std::map<int, std::array<std::string, 2>> DropDownDictionary;
const DropDownDictionary gNames{
    {1, {"A", "aaa"}},
    {2, {"B", "bbb"}},
    {3, {"C", "ccc"}}};

template <const DropDownDictionary &T>
struct MyCustomType
{
    std::string getDisplayLabelString()
    {
        // do somethings, just an example
        return T.begin()->second[1];
    }
    int getDisplayValueString()
    {
        // do somethings, just an example
        return T.begin()->first;
    }
};  

When I do somethings like this:

MyCustomStruct<MyCustomType<gNames>> cs;

It says the value of 'gNames' is not usable in a constant expression. But if I try to use constexpr, it seems I can't with std:map?

Solutions?

Aucun commentaire:

Enregistrer un commentaire