79798353

Date: 2025-10-24 03:41:12
Score: 0.5
Natty:
Report link

If handler is as:

class UiList
{
private:
    int m_CurrIndex;
    std::vector<std::string> m_Strs;
public:
    UiList(std::vector<std::string> strings, int initial_index=0 )
        : m_Strs( std::move(strings) ), m_CurrIndex(initial_index)
    {}

    void updateUi(){    displayString( m_Strs[m_CurrIndex] );   }
    void updateValue(int newIndex){ m_CurrIndex = newIndex; }

private:    //This is for test
    void displayString( const std::string &S ){ std::cout << S << std::endl;    }
};

template< class T >
constexpr size_t ListIndex(){   return 0;   }

template< class T >
constexpr int ToInt( T val ){   return static_cast<int>(val);   }

class MasterHanlder
{
private:
    std::vector< UiList > m_UiLists;

public:
    MasterHanlder( std::vector<UiList> UiLists )
        : m_UiLists( std::move( UiLists ) )
    {}

    template< class T >
    void Handle( T val )
    {
        auto &TgtUL = m_UiLists[ ListIndex<T>() ];
        TgtUL.updateValue( ToInt(val) );
        TgtUL.updateUi();
    }
};

Can we construct mapping as below?

enum class Colors{ red, green, blue };
enum class Animals{ cat, dog, goose };

//specialization for each enum
template<> constexpr size_t ListIndex<Colors>(){    return 0;   }
template<> constexpr size_t ListIndex<Animals>(){   return 1;   }

int main()
{
    MasterHanlder MH{
        {//The order should match the specialization above
            UiList{ { "red", "green", "blue" } },
            UiList{ { "cat", "dog", "goose" } }
        }
    };

    MH.Handle( Colors::blue );
    MH.Handle( Animals::cat );

    return 0;
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
Posted by: fana