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;
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
add a comment |
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
add a comment |
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
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
c++ lambda stdmap std-function
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
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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);
add a comment |
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&>
.
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 withstd::any
andany_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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
add a comment |
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);
add a comment |
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);
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);
answered Mar 27 at 9:48
CalethCaleth
21.4k2 gold badges24 silver badges44 bronze badges
21.4k2 gold badges24 silver badges44 bronze badges
add a comment |
add a comment |
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&>
.
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 withstd::any
andany_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
add a comment |
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&>
.
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 withstd::any
andany_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
add a comment |
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&>
.
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&>
.
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 withstd::any
andany_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
add a comment |
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 withstd::any
andany_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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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