mercredi 23 septembre 2015

using std::function in a std::map

I have a class...

#include <map>
#include <boost/function.hpp>

enum class ECmd { one, two, three };

class C
{
public:

    void Command(ECmd e)
    {
        auto pos = m_fnCmd.find(e);

        if (pos != m_fnCmd.end())
        {
            // call the function
            (pos->second)(this);
         }
        else
        {
            printf("no command!\n");
        }
    }

protected:
    using fnCmd = boost::function<void(C*)>;
    using fnCmdMap = std::map<ECmd, fnCmd>;

    static const fnCmdMap m_fnCmd;

    // the command functions
    void One() { printf("one.\n"); }
    void Two() { printf("Two.\n"); }
    void Three() { printf("Three.\n"); }
};

const C::fnCmdMap C::m_fnCmd =
{
    {std::make_pair(ECmd::one, &C::One)},
    {std::make_pair(ECmd::two, &C::Two)},
    {std::make_pair(ECmd::three, &C::Three)},
 };

Which demonstrates a technique i use for processing id based commands. This code works just fine, however when i change the class to use std::function instead of boost::function it fails to compile - does not like initializing C::m_fnCmd.

Anyone have any suggestions?

Thanks

Robin

Aucun commentaire:

Enregistrer un commentaire