How to have zero or more arguments to a std::function used as value in a std::map?Initializing a static std::map<int, int> in C++How to convert a std::string to const char* or char*?subscript operators for class with std::map member variableC++ “error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of …”Could not deduce template argument for std::function from std::bindC++11 class in std::map as Value with private constructorsc++ Adding Class Member Function into MapThe std::pair in the std::map is returned as constGetting into a mess with std::function and member function pointers : (How do I declare an iterator for a map with the following template - std::map<std::string, T> my_map?

How does "Te vas a cansar" mean "You're going to get tired"?

How to avoid the "need" to learn more before conducting research?

Not going forward with internship interview process

What is my malfunctioning AI harvesting from humans?

If "more guns less crime", how do gun advocates explain that the EU has less crime than the US?

Continuous vertical line using booktabs in tabularx table?

What does this double-treble double-bass staff mean?

ICO or PNG Format?

What are the uses and limitations of Persuasion, Insight, and Deception against other PCs?

How to take the beginning and end parts of a list with simpler syntax?

Is there a way to unplug the Raspberry pi safely without shutting down

What does Apple mean by "This may decrease battery life"?

How is this kind of structure made?

Is there a standardised way to check fake news?

Double redundancy for the Saturn V LVDC computer memory, how were disagreements resolved?

What does "sardine box" mean?

Why did Gandalf use a sword against the Balrog?

Different inverter (logic gate) symbols

Was the 2019 Lion King film made through motion capture?

How should an administrative assistant reply to student addressing them as "Professor" or "Doctor"?

What should I call bands of armed men in Medieval Times?

Does this Foo machine halt?

Can the ground attached to neutral fool a receptacle tester?

Should you play baroque pieces a semitone lower?



How to have zero or more arguments to a std::function used as value in a std::map?


Initializing a static std::map<int, int> in C++How to convert a std::string to const char* or char*?subscript operators for class with std::map member variableC++ “error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of …”Could not deduce template argument for std::function from std::bindC++11 class in std::map as Value with private constructorsc++ Adding Class Member Function into MapThe std::pair in the std::map is returned as constGetting into a mess with std::function and member function pointers : (How do I declare an iterator for a map with the following template - std::map<std::string, T> my_map?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have a Engine class which has a member of type std::map, that maps enum to member functions.
I'm able to map the enum to all the functions, if the number of arguments of the functions are same.



enum STATE OFF, ON, HIBERNATE, RESTART ;
enum RETCODE SUCCESS, FAIL, UNKNOWN, OUTOFBOUND ;

class Engine

Engine();
RETCODE StateOn(double val);
RETCODE StateOff(double val);
RETCODE StateHibernate(double val);
RETCODE StateRestart(double val);
private:
const std::map<STATE, std::function<RETCODE(double)>> Map_State_FnPtr;
;

Engine::Engine() : Map_State_FnPtr
STATE::ON, [=](double val) return StateOn(val); ,
STATE::OFF, [=](double val) return StateOff(val); ,
STATE::HIBERNATE, [=](double val) return StateHibernate(val); ,
STATE::RESTART, [=](double val) return StateRestart(val);


// c'tor body



But I have a scenario where some of the functions can have zero or multiple arguments.
How do I declare & construct the map variable in such scenario?



class Engine

Engine();
RETCODE StateOn(); // No arguments
RETCODE StateOff(double val); // 1 argument
RETCODE StateHibernate(double val, const std::string & data); // Multiple arguments
RETCODE StateRestart(double val);
private:
const std::unordered_map<STATE, std::function<RETCODE()>> Map_State_FnPtr; // What should be the declaration?
;


Any other suggestions for this scenario?
Kindly help.










share|improve this question
































    2















    I have a Engine class which has a member of type std::map, that maps enum to member functions.
    I'm able to map the enum to all the functions, if the number of arguments of the functions are same.



    enum STATE OFF, ON, HIBERNATE, RESTART ;
    enum RETCODE SUCCESS, FAIL, UNKNOWN, OUTOFBOUND ;

    class Engine

    Engine();
    RETCODE StateOn(double val);
    RETCODE StateOff(double val);
    RETCODE StateHibernate(double val);
    RETCODE StateRestart(double val);
    private:
    const std::map<STATE, std::function<RETCODE(double)>> Map_State_FnPtr;
    ;

    Engine::Engine() : Map_State_FnPtr
    STATE::ON, [=](double val) return StateOn(val); ,
    STATE::OFF, [=](double val) return StateOff(val); ,
    STATE::HIBERNATE, [=](double val) return StateHibernate(val); ,
    STATE::RESTART, [=](double val) return StateRestart(val);


    // c'tor body



    But I have a scenario where some of the functions can have zero or multiple arguments.
    How do I declare & construct the map variable in such scenario?



    class Engine

    Engine();
    RETCODE StateOn(); // No arguments
    RETCODE StateOff(double val); // 1 argument
    RETCODE StateHibernate(double val, const std::string & data); // Multiple arguments
    RETCODE StateRestart(double val);
    private:
    const std::unordered_map<STATE, std::function<RETCODE()>> Map_State_FnPtr; // What should be the declaration?
    ;


    Any other suggestions for this scenario?
    Kindly help.










    share|improve this question




























      2












      2








      2








      I have a Engine class which has a member of type std::map, that maps enum to member functions.
      I'm able to map the enum to all the functions, if the number of arguments of the functions are same.



      enum STATE OFF, ON, HIBERNATE, RESTART ;
      enum RETCODE SUCCESS, FAIL, UNKNOWN, OUTOFBOUND ;

      class Engine

      Engine();
      RETCODE StateOn(double val);
      RETCODE StateOff(double val);
      RETCODE StateHibernate(double val);
      RETCODE StateRestart(double val);
      private:
      const std::map<STATE, std::function<RETCODE(double)>> Map_State_FnPtr;
      ;

      Engine::Engine() : Map_State_FnPtr
      STATE::ON, [=](double val) return StateOn(val); ,
      STATE::OFF, [=](double val) return StateOff(val); ,
      STATE::HIBERNATE, [=](double val) return StateHibernate(val); ,
      STATE::RESTART, [=](double val) return StateRestart(val);


      // c'tor body



      But I have a scenario where some of the functions can have zero or multiple arguments.
      How do I declare & construct the map variable in such scenario?



      class Engine

      Engine();
      RETCODE StateOn(); // No arguments
      RETCODE StateOff(double val); // 1 argument
      RETCODE StateHibernate(double val, const std::string & data); // Multiple arguments
      RETCODE StateRestart(double val);
      private:
      const std::unordered_map<STATE, std::function<RETCODE()>> Map_State_FnPtr; // What should be the declaration?
      ;


      Any other suggestions for this scenario?
      Kindly help.










      share|improve this question
















      I have a Engine class which has a member of type std::map, that maps enum to member functions.
      I'm able to map the enum to all the functions, if the number of arguments of the functions are same.



      enum STATE OFF, ON, HIBERNATE, RESTART ;
      enum RETCODE SUCCESS, FAIL, UNKNOWN, OUTOFBOUND ;

      class Engine

      Engine();
      RETCODE StateOn(double val);
      RETCODE StateOff(double val);
      RETCODE StateHibernate(double val);
      RETCODE StateRestart(double val);
      private:
      const std::map<STATE, std::function<RETCODE(double)>> Map_State_FnPtr;
      ;

      Engine::Engine() : Map_State_FnPtr
      STATE::ON, [=](double val) return StateOn(val); ,
      STATE::OFF, [=](double val) return StateOff(val); ,
      STATE::HIBERNATE, [=](double val) return StateHibernate(val); ,
      STATE::RESTART, [=](double val) return StateRestart(val);


      // c'tor body



      But I have a scenario where some of the functions can have zero or multiple arguments.
      How do I declare & construct the map variable in such scenario?



      class Engine

      Engine();
      RETCODE StateOn(); // No arguments
      RETCODE StateOff(double val); // 1 argument
      RETCODE StateHibernate(double val, const std::string & data); // Multiple arguments
      RETCODE StateRestart(double val);
      private:
      const std::unordered_map<STATE, std::function<RETCODE()>> Map_State_FnPtr; // What should be the declaration?
      ;


      Any other suggestions for this scenario?
      Kindly help.







      c++ lambda stdmap std-function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 8:53







      Rahul

















      asked Mar 27 at 8:32









      RahulRahul

      5102 gold badges8 silver badges22 bronze badges




      5102 gold badges8 silver badges22 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          2














          You can always ignore parameters




          STATE::ON, [=](double, const std::string &) return StateOn(); ,
          STATE::OFF, [=](double val, const std::string &) return StateOff(val); ,
          STATE::HIBERNATE, [=](double val, const std::string & data) return StateHibernate(val, data); ,
          STATE::RESTART, [=](double val, const std::string &) return StateRestart(val);






          share|improve this answer
































            2















            How do I declare & construct the map variable in such scenario?




            This is impossible with std::function. This template offers a certain amount of type erasure, as it can be constructed with function pointers, stateful lambdas or pointer to member functions, but there is an invariant, i.e., the signature of the function type that you approximate with the std::function instance.



            You could instead define a custom aggregate that embodies possible variations of parameter lists, e.g.



            struct StateParams 
            double val;
            std::string data;
            ;


            then change your function signatures to



            RETCODE StateOn(const StateParams&);
            RETCODE StateHibernate(const StateParams&);
            // ...


            and the std::map can have a value_type std::function<RETCODE(const StatemParams&>.






            share|improve this answer

























            • If not by using std::function, how can I declare/construct a std::map to suit the following requirement. Other alternative suggestions are welcome.

              – Rahul
              Mar 27 at 8:48











            • You could try going with std::any and any_cast if you have C++17 available, but this requires an additional field to remember the actual signature, and it's also quite unsafe. There is no easy way to do this.

              – lubgr
              Mar 27 at 8:54













            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372757%2fhow-to-have-zero-or-more-arguments-to-a-stdfunction-used-as-value-in-a-stdma%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You can always ignore parameters




            STATE::ON, [=](double, const std::string &) return StateOn(); ,
            STATE::OFF, [=](double val, const std::string &) return StateOff(val); ,
            STATE::HIBERNATE, [=](double val, const std::string & data) return StateHibernate(val, data); ,
            STATE::RESTART, [=](double val, const std::string &) return StateRestart(val);






            share|improve this answer





























              2














              You can always ignore parameters




              STATE::ON, [=](double, const std::string &) return StateOn(); ,
              STATE::OFF, [=](double val, const std::string &) return StateOff(val); ,
              STATE::HIBERNATE, [=](double val, const std::string & data) return StateHibernate(val, data); ,
              STATE::RESTART, [=](double val, const std::string &) return StateRestart(val);






              share|improve this answer



























                2












                2








                2







                You can always ignore parameters




                STATE::ON, [=](double, const std::string &) return StateOn(); ,
                STATE::OFF, [=](double val, const std::string &) return StateOff(val); ,
                STATE::HIBERNATE, [=](double val, const std::string & data) return StateHibernate(val, data); ,
                STATE::RESTART, [=](double val, const std::string &) return StateRestart(val);






                share|improve this answer













                You can always ignore parameters




                STATE::ON, [=](double, const std::string &) return StateOn(); ,
                STATE::OFF, [=](double val, const std::string &) return StateOff(val); ,
                STATE::HIBERNATE, [=](double val, const std::string & data) return StateHibernate(val, data); ,
                STATE::RESTART, [=](double val, const std::string &) return StateRestart(val);







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 9:48









                CalethCaleth

                21.4k2 gold badges24 silver badges44 bronze badges




                21.4k2 gold badges24 silver badges44 bronze badges


























                    2















                    How do I declare & construct the map variable in such scenario?




                    This is impossible with std::function. This template offers a certain amount of type erasure, as it can be constructed with function pointers, stateful lambdas or pointer to member functions, but there is an invariant, i.e., the signature of the function type that you approximate with the std::function instance.



                    You could instead define a custom aggregate that embodies possible variations of parameter lists, e.g.



                    struct StateParams 
                    double val;
                    std::string data;
                    ;


                    then change your function signatures to



                    RETCODE StateOn(const StateParams&);
                    RETCODE StateHibernate(const StateParams&);
                    // ...


                    and the std::map can have a value_type std::function<RETCODE(const StatemParams&>.






                    share|improve this answer

























                    • If not by using std::function, how can I declare/construct a std::map to suit the following requirement. Other alternative suggestions are welcome.

                      – Rahul
                      Mar 27 at 8:48











                    • You could try going with std::any and any_cast if you have C++17 available, but this requires an additional field to remember the actual signature, and it's also quite unsafe. There is no easy way to do this.

                      – lubgr
                      Mar 27 at 8:54















                    2















                    How do I declare & construct the map variable in such scenario?




                    This is impossible with std::function. This template offers a certain amount of type erasure, as it can be constructed with function pointers, stateful lambdas or pointer to member functions, but there is an invariant, i.e., the signature of the function type that you approximate with the std::function instance.



                    You could instead define a custom aggregate that embodies possible variations of parameter lists, e.g.



                    struct StateParams 
                    double val;
                    std::string data;
                    ;


                    then change your function signatures to



                    RETCODE StateOn(const StateParams&);
                    RETCODE StateHibernate(const StateParams&);
                    // ...


                    and the std::map can have a value_type std::function<RETCODE(const StatemParams&>.






                    share|improve this answer

























                    • If not by using std::function, how can I declare/construct a std::map to suit the following requirement. Other alternative suggestions are welcome.

                      – Rahul
                      Mar 27 at 8:48











                    • You could try going with std::any and any_cast if you have C++17 available, but this requires an additional field to remember the actual signature, and it's also quite unsafe. There is no easy way to do this.

                      – lubgr
                      Mar 27 at 8:54













                    2












                    2








                    2








                    How do I declare & construct the map variable in such scenario?




                    This is impossible with std::function. This template offers a certain amount of type erasure, as it can be constructed with function pointers, stateful lambdas or pointer to member functions, but there is an invariant, i.e., the signature of the function type that you approximate with the std::function instance.



                    You could instead define a custom aggregate that embodies possible variations of parameter lists, e.g.



                    struct StateParams 
                    double val;
                    std::string data;
                    ;


                    then change your function signatures to



                    RETCODE StateOn(const StateParams&);
                    RETCODE StateHibernate(const StateParams&);
                    // ...


                    and the std::map can have a value_type std::function<RETCODE(const StatemParams&>.






                    share|improve this answer














                    How do I declare & construct the map variable in such scenario?




                    This is impossible with std::function. This template offers a certain amount of type erasure, as it can be constructed with function pointers, stateful lambdas or pointer to member functions, but there is an invariant, i.e., the signature of the function type that you approximate with the std::function instance.



                    You could instead define a custom aggregate that embodies possible variations of parameter lists, e.g.



                    struct StateParams 
                    double val;
                    std::string data;
                    ;


                    then change your function signatures to



                    RETCODE StateOn(const StateParams&);
                    RETCODE StateHibernate(const StateParams&);
                    // ...


                    and the std::map can have a value_type std::function<RETCODE(const StatemParams&>.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 27 at 8:45









                    lubgrlubgr

                    24.3k3 gold badges32 silver badges77 bronze badges




                    24.3k3 gold badges32 silver badges77 bronze badges















                    • If not by using std::function, how can I declare/construct a std::map to suit the following requirement. Other alternative suggestions are welcome.

                      – Rahul
                      Mar 27 at 8:48











                    • You could try going with std::any and any_cast if you have C++17 available, but this requires an additional field to remember the actual signature, and it's also quite unsafe. There is no easy way to do this.

                      – lubgr
                      Mar 27 at 8:54

















                    • If not by using std::function, how can I declare/construct a std::map to suit the following requirement. Other alternative suggestions are welcome.

                      – Rahul
                      Mar 27 at 8:48











                    • You could try going with std::any and any_cast if you have C++17 available, but this requires an additional field to remember the actual signature, and it's also quite unsafe. There is no easy way to do this.

                      – lubgr
                      Mar 27 at 8:54
















                    If not by using std::function, how can I declare/construct a std::map to suit the following requirement. Other alternative suggestions are welcome.

                    – Rahul
                    Mar 27 at 8:48





                    If not by using std::function, how can I declare/construct a std::map to suit the following requirement. Other alternative suggestions are welcome.

                    – Rahul
                    Mar 27 at 8:48













                    You could try going with std::any and any_cast if you have C++17 available, but this requires an additional field to remember the actual signature, and it's also quite unsafe. There is no easy way to do this.

                    – lubgr
                    Mar 27 at 8:54





                    You could try going with std::any and any_cast if you have C++17 available, but this requires an additional field to remember the actual signature, and it's also quite unsafe. There is no easy way to do this.

                    – lubgr
                    Mar 27 at 8:54

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55372757%2fhow-to-have-zero-or-more-arguments-to-a-stdfunction-used-as-value-in-a-stdma%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript