0

i trying to develop a Finite State Machine with template meta programming techniques, but i getting stuck with a map that it has to be fill at compile time, this the code(gcc 4.8 c++11):

    #include <functional>
    #include <type_traits>
    #include <iostream>
    #include <unordered_map>
    namespace NSStateMachine {
    //Definicion de estado unidad

    template<class FSM, class From, class Event, class TO, bool(FSM::* transicion_fn)(const Event &)>
    struct Transition
    {
        using FSM_TYPE=FSM;
        using FROM_STATE= From;
        using EVENT_TYPE= Event;
        using TO_STATE_TYPE=TO;
        using EVENT_BASE_TYPE = typename Event::BASE_TYPE;

        static  bool do_transition(FSM_TYPE& currenState, EVENT_BASE_TYPE const& aEvent)
        {
            return (currenState.*transicion_fn)(static_cast<EVENT_TYPE const&>(aEvent));
        }
    };

    //States
    template<class Transition, const Transition * const TransitionPtr, class ... Args>
    class StateMachine
    {
      public:
        StateMachine():transitionMap{{static_cast<typename Transition::TransitionID>(*TransitionPtr::TransitionID_Value),nullptr}}
        {}
        template<class Event>
        bool evalEvent(const Event & aEvent)
        {
            std::cout<<"evento recibido"<<std::endl;
        }

        std::unordered_map<typename Transition::TransitionID, const Transition * const > transitionMap ;

    };
    }
int main()
{
//it doesnt compile, i canoot create the state machine
return 0;
}

The compile error:

 error: 'TransitionPtr' is not a class, namespace, or enumeration
             StateMachine():transitionMap{{static_cast<typename Transition::TransitionID>(*TransitionPtr::TransitionID_Value),nullptr}}


                                                                       ^

The problem seem to be in the line

transitionMap{{static_cast<typename Transition::TransitionID>(*TransitionPtr::TransitionID_Value),nullptr}}

i will try to init the unorderer_map with the automatic constructor. i have defined this Transition::TransitionID as a class variable defined in the class represented by the template argument

I will really appreciate any help. Thx!!!!

i have already test with default types , it compile and work this

Ricardo_arg
  • 520
  • 11
  • 28

1 Answers1

2

The error message is pretty clear. TransitionPtr is a pointer, not a type, so you can't use it to the left of :: in TransitionPtr::TransitionID_Value.

Also, I don't think you'll find a way to initialize an unordered_set at compile time, since it doesn't have constexpr constructors and in general almost certainly uses heap allocations.

aschepler
  • 70,891
  • 9
  • 107
  • 161