1

How can I get this type of usage out of my C++ compiler? Consider a very simple hierarchical state-machine, where you can specify the states are unique enum types (enum class). Here's some use-case pseudo code:

    enum class lev0
    {
        start,
        end
    };

    enum class lev1
    {
        start,
        end
    };

    enum class lev2
    {
        start,
        end
    };

    HSMSimple<lev0, lev1, lev2> hsm_lev0;
    const HSMSimple<lev1, lev2>& hsm_lev1 = hsm_lev0.nextLevel;
    const HSMSimple<lev2>& hsm_lev2 = hsm_lev1.nextLevel;

    switch (hsm_lev0)
    {
    case lev0::start:
        switch (hsm_lev1)
        {
        case lev1::start:
            switch (hsm_lev2)
            {
            case lev2::start:
                break;
            case lev2::end:
                break;
            }
            break;
        case lev1::end:
            break;
        }
        break;
    case lev0::end:
        break;
    }
    ...

Ideas? I've tried a class as such:

    template<typename arg1, typename ... TArgs>
    class HSMSimple
    {
    public:
        operator const arg1&() const { return m_lev1; }
        operator arg1() { return m_lev1; }

        const HSMSimple<TArgs>& nextLev() const { return m_nextLevel; }
        HSMSimple<TArgs> nextLev() { return m_nextLevel; }

    protected:
        arg1 m_lev1;
        HSMSimple<TArgs> m_nextLevel;
    };

But the compiler says an instantiation must pack the args at that last line in the class. When I try writing 'HSMSimple< TArgs...> m_nextLevel;' just more chaos ensues. I feel like more trickery is needed, but can't figure out what.

subotai
  • 11
  • 2
  • [This](https://stackoverflow.com/questions/7124969/recursive-variadic-template-to-print-out-the-contents-of-a-parameter-pack) might be of some help – user3738870 Sep 30 '18 at 19:37
  • That link only discusses wrt functions, which for some reason seem to be easier to wrap my head around. My question is wrt purely class variadic templates. It feels like I just need a way to 'end' the TArgs somehow in the class. I wonder if its better to approach this by thinking how 'm_nextLevel' might use the 'is a' pattern, vs the 'has a' pattern that it currently is... – subotai Oct 01 '18 at 21:49
  • Yes, maybe try specializing your class with an empty template argument like this: `template<> class HSMSimple<> { ... }; ` – user3738870 Oct 02 '18 at 17:49

0 Answers0