'Automatically get nested state classes to use in std::variant variadic parameter for state pattern

My problem is this: I have a class which stores a std::variant of state classes (state pattern). The states themselves are private nested classes of the class which owns this std::variant.

class Foo
{
private:
    class OnState
    {
        // …
    }

    class OffState
    {
        // …
    }

    std::variant<StatesTs…> m_state;
}

I want to at, compile time, give all the nested state classes to the variadic parameter of the std::variant, without having to manually maintain a list of state types. I want to be able to create a new nested state class and have it automatically become a parameter to the std::variant’s parameter pack.

Thus far, I have identified that this might be able to be solved using the Curiously Recurring Template Pattern, although forgive me for my knowledge on this pattern and it’s capabilities is very limited.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source