Beginner template compile error - Having trouble passing function as arg - c++

Most of my classes have lists as private data members. As such, most of them also have adder/remover public members. I noticed I was writing basically the same thing for each of the adders, so I thought I'd turn it into a template.
Here's what I've got:
template <class N, class I>
void add(N element, std::list<N> & container, I (*f)(void),
std::string successmsg, std::string exceptmsg) {
typename std::list<N>::iterator it;
it = find(container.begin(), container.end(), element);
try {
if(it != container.end())
throw DuplicateElement<I>(element->(*f));
}
catch (DuplicateElement<I>&){
std::cout << exceptmsg;
pause(PAUSE_MESSAGE);
return;
}
container.push_back(element);
std::cout << successmsg;
}
Let me just explain this one, its parameters (in order) are the element to add, the container which the element shall be added to, a function present on the element class that returns its unique ID (a code, a license plate, whatever), a success message and an exception message.
Sample usage:
void Aeroporto::addCompanhia(CompanhiaAerea* companhia) {
std::string success = "Companhia aérea adicionada com sucesso!";
std::string except = "Companhia aérea já existente!";
add(companhia, companhias, getSigla, success, except);
// std::list<CompanhiaAerea*>::iterator it;
//
// it = find(companhias.begin(), companhias.end(), companhia);
// try{
// if(it != companhias.end())
// throw DuplicateElement<std::string>(companhia->getSigla());
// }
// catch (DuplicateElement<std::string>&){
// std::cout << "Companhia aérea já existente!";
// pause(PAUSE_MESSAGE);
// return;
// }
//
// companhias.push_back(companhia);
// std::cout << "Companhia aérea adicionada com sucesso!";
}
When I try to compile this, I get the following:
..\src\/headers/template.h: In function 'void add(N, std::list<N>&, I (*)(), std::string, std::string)':
..\src\/headers/template.h:23:29: error: expected primary-expression before '(' token
..\src\/headers/template.h:23:39: error: expected unqualified-id before '(' token
..\src\aeroporto.cpp: In member function 'void Aeroporto::addCompanhia(CompanhiaAerea*)':
..\src\aeroporto.cpp:76:54: error: no matching function for call to 'add(CompanhiaAerea*&, std::list<CompanhiaAerea*>&, <unresolved overloaded function type>, std::string&, std::string&)'
I require your assistance since it's not particularly easy to google for this kind of stuff.
Thank you for your time!

Because you want to use a method, you have to use a pointer-to-member-function:
template <class N, class I>
void add(N* element, std::list<N*>& container, I (N::*f)() const,
std::string successmsg, std::string exceptmsg) {
typename std::list<N*>::iterator it;
it = find(container.begin(), container.end(), element);
try {
if(it != container.end())
throw DuplicateElement<I>((element->*f)());
//...
//...
add(companhia, companhias, &CompanhiaAerea::getSigla, success, except);
//...
Note how I had to change the way N is used, in order to get the right type for f.

You do not want to pass a function pointer, but a member function pointer. They are quite different in C++ (basically the implicit this argument). This is a simple test case on how free functions, static member functions and non-static member functions can be passed around. You will have to complete it with the template part:
struct test {
void foo() { std::cout << "test::foo" << std::endl; }
static void bar() { std::cout << "test::bar" << std::endl; }
};
void foo() { std::cout << "foo" << std::endl; }
void call_function( void (*f)() )
{
f();
}
void call_member( test & t, void (test::*f)() )
{
t.*f();
(&t)->*f();
}
int main()
{
test t;
call_function( &foo ); // free function
call_function( &test::bar ); // equivalent for static member function
call_member( t, &test::foo ); // but not for member function
}

Related

c++ callbacks to another member function

I have a question on callbacks. Previously, I am associating my callbacks to a class Q
class Q{
using Callback = std::function<void(char*, int)>;
Q:Q();
Q:~Q();
void Q::RegisterCB(Callback callbackfunc)
{
callback_func = callbackfunc;
}
void Q:someEvent()
{
callback_func();
}
};
void handleCallback( char*, int)
{
// perform some routine
}
// from my main file
int main()
{
Q q;
q.RegisterCB(&handleCallback);
}
It works well for me. However, when I need to transfer the handleCallback function to another class for cleaner code. I have problem with using same code
class R{
void R::handleCallback( char*, int)
{
// perform some routine
}
void R::someOp()
{
// q is some member variables of R
q.RegisterCB(&R::handleCallback, this);
}
};
However, i run into some problems of saying there is a "no matching function for call to .....". I thought it was just simply assigning from function name to class function name
May I have a hint to where I might go wrong?
Regards
&R::handleCallback has the type void (R::*)(char*, int), which is not convertible to std::function<void(char*, int)>.
Also, RegisterCB takes one argument, not two.
The most straightforward fix is to wrap the call in a lambda function,
q.RegisterCB([this](char* p, int x) { handleCallback(p, x); });
Example on how to use a lambda function to register a member function of an instance of R as event handler. (I replaced char* with string_view out of habit, it's not essential for this example). The use of "const" wherever you can is a recommendation.
#include <functional>
#include <string_view>
#include <iostream>
class Q
{
public:
// use const arguments, the callback is not supposed to change them
// just passing information on to callback
using callback_t = std::function<void(const std::string_view&, const int)>;
// initialize callback with a (lambda) function that does nothing
// this prevents the need for a check if callback has been set or not
// (Pattern : Null Strategy)
Q() :
m_callback_func( [](const std::string_view&,const int) {} )
{
}
~Q() = default;
void RegisterCallback(callback_t fn)
{
m_callback_func = fn;
}
void Event(const std::string_view& string, const int value)
{
m_callback_func(string,value);
}
private:
callback_t m_callback_func;
};
void handleCallback(const std::string_view& string, const int value)
{
std::cout << string << ", " << value << "\n";
}
class R
{
public:
void handleCallback(const std::string_view& string, const int value)
{
std::cout << string << ", " << value << "\n";
}
};
// from my main file
int main()
{
Q q1;
q1.RegisterCallback(handleCallback);
q1.Event("Hello", 42);
// to pass a callback to an instance of a class
// you can use a lambda function https://en.cppreference.com/w/cpp/language/lambda
R r;
Q q2;
q2.RegisterCallback([&r](const std::string_view& string, const int value)
{
r.handleCallback(string,value);
});
q2.Event("World",21);
return 0;
}

How can I pass a member function with an unknown prototype to a class in C++?

I need to make a class (we'll call it Command) that takes in a string, processes it into function arguments, and then passes it to a member function of a different class. For my use, the member function that I pass to Command could come from a number of classes, and could have many different prototypes. I can guarantee that that member function will return void. Here's the code I imagine:
class Command {
public:
vector<tuple<int, string, any>> argument_specification;
SomeType callable;
Command(vector<tuple<int, string, any>> argument_spec, SomeType callable) {
this->argument_specification = argument_spec;
this->callable = callable;
}
void apply(string args) {
/* processing args according to this->argument_specification
to make a std::tuple arguments */
std::apply(this->callable, arguments);
}
};
class Action {
public:
print_two_arguments(int arg1, int arg2) {
std::cout << arg1 << ", " << arg2 << std::endl;
}
print_one_arguments(std::string arg1) {
std::cout << arg1 << std::endl);
}
}
int main() {
Action *actor = new Action();
// my argument specification code splits by string and then extracts
// arguments by position or keyword and replacing with a default if
// not specified
Command *command1 = new Command({{0, "first_arg", "something"}},
&actor->print_one_argument);
command1->apply("hello_world"); // Should print "hello_world"
Command *command2 = new Command({{0, "first_arg", 2},
{1, "second_arg", 10}},
&actor->print_two_arguments);
command2->apply("0 2"); // should print "0 2"
}
I don't really mind what method gets there - I've tried std::bind and can't quite get that to work, I've also tried lambdas. I'm currently trying a template class with a type deduced factory method. I'm also open to a macro definition that will fix this at compile time.
A couple ideas come to mind, but the key thing that I'm seeing is that you want to be able to take an arbitrary void function and call it with a single string. Templates can be really helpful here because you can use them to auto-deduce things such as how to build the tuple that you apply to the function.
This will be a semi-complicated meta-program-y solution, but I love that stuff; so I'm going to build a prototype. Also beware, this is the kind of solution that will result in absolutely horrendous compiler errors if you try to use it wrong.
My suggestion would be to make Command a templated type, where the command itself is templated on the parameter types of the function you want to pass it. If you need to be able to make a list of these to apply arguments to, then you can have a base class which provides the apply function. Since I don't fully understand how the argument specification is supposed to work, I'm punting on that and supporting keyword arguments only; but the way I built this, it should be fairly straightfoward to sub in your own argument splitter. I think. It could be cleaner, but I need to get back to my job.
Play with it on Compiler Explorer: https://godbolt.org/z/qqrn9bs1T
#include <any>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <memory>
#include <regex>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
// Converts the string arguments to the actual types
template <class T> T convert_arg(std::string);
template <> std::string convert_arg<std::string>(std::string s) { return s; }
template <> int convert_arg<int>(std::string s) { return std::stoi(s); }
// Split on spaces
std::vector<string> tokenize(std::string s) {
istringstream iss(s);
return {istream_iterator<string>{iss}, istream_iterator<string>{}};
}
// Argument spec defines how to parse the arguments from the input. It
// contains the positional index in the string, the name of it, and a
// default value. It's effectively a mapping from the string being applied
// to the function being called.
//
// This could maybe be turned into a std::tuple<std::tuple<...>>, but
// I'm not sure. That could get a little messy with trying to iterate
// through it to build the argument list, and I don't think it buys us
// anything.
//
// For example, given the argument spec
// {{1, "first_arg", 0}, {0, "second_arg", "some_default"}}
// You could call a function that has the signature
// void (int, string);
// And you could parse the following argument strings (assuming space-delimited)
// "second_arg=hello first_arg=0"
// "words 1"
// "first_arg=5 more_text"
using argument_spec_t = std::vector<tuple<std::size_t, string, std::string>>;
class CommandBase {
public:
virtual void apply(string args) = 0;
};
// Concrete commands are templated on the argument types of the function
// that they will invoke. For best results, use make_command() to deduce
// this template from the function that you want to pass the Command in
// order to get references and forwarding correct.
template <class... ArgTs> class Command : public CommandBase {
public:
using callable_t = std::function<void(ArgTs...)>;
// Holds the argument specification given during constuction; this
// indicates how to parse the string arguments
argument_spec_t m_argument_specification;
// A function which can be invoked
callable_t m_callable;
Command(argument_spec_t argument_spec, callable_t callable)
: m_argument_specification(std::move(argument_spec)),
m_callable(std::move(callable)) {}
void apply(string args) {
//std::cout << "Apply " << args << std::endl;
std::tuple parsed_args =
build_args(split_args(std::move(args), m_argument_specification),
std::index_sequence_for<ArgTs...>{});
std::apply(m_callable, parsed_args);
}
private:
// Pre-processes the command arguments string into a
// std::unordered_map<size_t, std::string> where x[i] returns the text of the
// i'th argument to be passed to the function.
//
// \todo Support positional arguments
// \todo Be more robust
static std::unordered_map<size_t, std::string>
split_args(std::string args, const argument_spec_t &arg_spec) {
std::unordered_map<std::string, std::string> kw_args;
std::unordered_map<size_t, std::string> arg_map;
vector<string> tokens = tokenize(args);
for (const auto &token : tokens) {
auto delim = token.find("=");
auto key = token.substr(0, delim);
auto val = token.substr(delim + 1);
kw_args[key] = val;
// std::cout << "key = " << val << std::endl;
}
for (size_t i = 0; i < arg_spec.size(); ++i) {
const auto &[pos_index, key, default_val] = arg_spec[i];
auto given_arg_it = kw_args.find(key);
if (given_arg_it != kw_args.end())
arg_map[i] = given_arg_it->second;
else
arg_map[i] = default_val;
// std::cout << i << " -> " << arg_map[i] << std::endl;
}
return arg_map;
}
// Copies the arguments from the map returned by pre_process_args into a
// std::tuple which can be used with std::apply to call the internal function.
// This uses a faux fold operation because I'm not sure the right way to do a
// fold in more modern C++
// https://articles.emptycrate.com/2016/05/14/folds_in_cpp11_ish.html
template <std::size_t... Index>
std::tuple<ArgTs...>
build_args(std::unordered_map<size_t, std::string> arg_map,
std::index_sequence<Index...>) {
std::tuple<ArgTs...> args;
std::initializer_list<int> _{
(std::get<Index>(args) =
convert_arg<std::tuple_element_t<Index, std::tuple<ArgTs...>>>(
std::move(arg_map[Index])),
0)...};
return args;
}
};
// Factory function to make a command which calls a pointer-to-member
// function. It's important that the reference to the object stays in
// scope as long as the Command object returned!
template <class C, class... ArgTs>
std::unique_ptr<CommandBase> make_command(C &obj,
void (C::*member_function)(ArgTs...),
argument_spec_t argument_spec) {
return std::make_unique<Command<ArgTs...>>(
std::move(argument_spec), [&obj, member_function](ArgTs... args) {
(obj.*member_function)(std::forward<ArgTs>(args)...);
});
}
// Factory function to make a command which calls a std::function.
template <class... ArgTs>
std::unique_ptr<CommandBase>
make_command(std::function<void(ArgTs...)> callable,
argument_spec_t argument_spec) {
return std::make_unique<Command<ArgTs...>>(std::move(argument_spec),
std::move(callable));
}
// Factory function to make a command which calls a free function
template <class... ArgTs>
std::unique_ptr<CommandBase> make_command(void (*fn)(ArgTs...),
argument_spec_t argument_spec) {
return make_command(std::function<void(ArgTs...)>{fn},
std::move(argument_spec));
}
class Action {
public:
void print_two_arguments(int arg1, int arg2) {
std::cout << arg1 << ", " << arg2 << std::endl;
}
void print_one_argument(std::string arg1) { std::cout << arg1 << std::endl; }
};
void print_one_argument_free(std::string arg1) {
std::cout << arg1 << std::endl;
}
int main() {
Action actor;
// my argument specification code splits by string and then extracts
// arguments by position or keyword and replacing with a default if
// not specified
auto command1 = make_command(actor, &Action::print_one_argument,
argument_spec_t{{0, "first_arg", "something"}});
command1->apply("first_arg=hello_world"); // Should print "hello_world"
auto command2 = make_command(
actor, &Action::print_two_arguments,
argument_spec_t{{0, "first_arg", "2"}, {1, "second_arg", "10"}});
command2->apply("0 second_arg=2"); // should print "0 2"*/
auto command3 = make_command(&print_one_argument_free,
argument_spec_t{{0, "first_arg", "something"}});
command3->apply("first_arg=hello_again");
}
I think there are a number of ways to handle this problem, including function pointers with variable arguments, etc. But your fundamental problem is that you're asking one class to understand the internals of another class, which never works out well. I'd argue instead that you should have a parent Actor class that has a function that can be overridden by sub-classes and just passing an instance of the subclass instead. Each subclass may need to take an array of arguments, or even another container type that each subclass knows what it needs from within.
#include <iostream>
using namespace std;
class Data {
public:
std::string strdata;
int intinfo1;
int intinfo2;
};
class ActionBase {
public:
virtual void act(Data d) = 0;
};
class PrintIntinfos : public ActionBase {
public:
virtual void act(Data d) {
std::cout << d.intinfo1 << ", " << d.intinfo2 << std::endl;
}
};
class PrintStrData : public ActionBase {
public:
virtual void act(Data d) {
std::cout << d.strdata << std::endl;
}
};
int main()
{
ActionBase *Action1 = new PrintIntinfos();
Data d = Data();
d.intinfo1 = 42;
d.intinfo2 = -42;
Action1->act(d);
delete Action1;
d.strdata = "hello world";
Action1 = new PrintStrData();
Action1->act(d);
}
What you should actually do requires analysis of what your goals are with respect to base-pointers and containers and your data structure, flow, etc.
In your apply you describe something that really wants the context of the constructor. What if Command was
class Command {
std::function<void(std::string)> callable;
public:
template <typename... Args>
Command(std::function<std::tuple<Args...>(std::string)> argument_spec, std::function<void(Args...)> callable)
: callable([=](std::string args) { std::apply(callable, argument_spec(args)); })
{ }
void apply(std::string args) {
callable(args);
}
};
You would still be able to use your argument specification code to create the argument_spec parameter

How emulate a templatised std::function in C++

Following is a basic instance of what I am doing in my C++ program. I have a list of listeners which are all std::functions. I have a concept DataType which means what kind of data the listener is interested in. The idea here is the same as publish-subscribe pattern. A method interested in certain kind of data should be able to add itself to the list of listeners using AddListener. Some methods are added & they receive a callback whenever required.
The program works fine !!
#include <iostream>
#include <functional>
#include <vector>
#include <string>
enum class DataType {
Type_1,
Type_2
// and so on
};
typedef std::function<void(std::pair<DataType, std::string>)> MyListenerType;
//template <typename T>
//typedef std::function<void(T>)> MyListenerType;
// How can I emulate the above so that a method passing any kind of primitive data-type namely "int, bool, float or double" can be added into
// my vector of listners.
std::vector<MyListenerType> my_data_listeners_1;
std::vector<MyListenerType> my_data_listeners_2;
void ListenerMethod_Instance_1(std::pair<DataType, std::string> information) {
DataType data_type = information.first;
std::string message = information.second;
std::cout << "ListenerMethod_Instance_1 called with message " << message << "\n";
}
void ListenerMethod_Instance_2(std::pair<DataType, std::string> information) {
DataType data_type = information.first;
std::string message = information.second;
std::cout << "ListenerMethod_Instance_2 called with message " << message << "\n";
}
void AddListener (MyListenerType listener, DataType type_of_interest) {
if (DataType::Type_1 == type_of_interest) {
my_data_listeners_1.push_back(listener);
std::cout << "Added a method instance for DataType::Type_1" << "\n";
}
else if (DataType::Type_2 == type_of_interest) {
my_data_listeners_2.push_back(listener);
std::cout << "Added a method instance for DataType::Type_2" << "\n";
}
else {
std::cout << "Listener type not supported" << "\n";
}
}
void CallAllListnersWhohaveSuscribed() {
if (!my_data_listeners_1.empty()) {
std::string send_message_1 = "some message 123";
std::pair <DataType, std::string> info_to_send_1 = std::make_pair (DataType::Type_1, send_message_1);
for(auto const &listener : my_data_listeners_1) {
listener(info_to_send_1);
}
}
if (!my_data_listeners_2.empty()) {
std::string send_message_2 = "some message 456";
std::pair <DataType, std::string> info_to_send_2 = std::make_pair (DataType::Type_2, send_message_2);
for(auto const &listener : my_data_listeners_2) {
listener(info_to_send_2);
}
}
}
int main() {
// Add ListenerMethod_Instance_1 for instance
DataType data_type_1 = DataType::Type_1;
auto listener_instance_1 = std::bind(ListenerMethod_Instance_1, std::placeholders::_1);
AddListener(listener_instance_1, data_type_1);
// Add ListenerMethod_Instance_2 for instance
DataType data_type_2 = DataType::Type_2;
auto listener_instance_2 = std::bind(ListenerMethod_Instance_2, std::placeholders::_1);
AddListener(listener_instance_2, data_type_2);
CallAllListnersWhohaveSuscribed();
return 0;
}
Following is the output of the program:
./stdFunctionTest
Added a method instance for DataType::Type_1
Added a method instance for DataType::Type_2
ListenerMethod_Instance_1 called with message some message 123
ListenerMethod_Instance_2 called with message some message 456
But here is how I want to modify & struggling with. The caveat is that every ListenerMethod_Instance_1 & ListenerMethod_Instance_2 have to parse the pair to get their info which I don't want to. I want to enable a method of any C++ primitive data type be it "int, bool, float or double" to be able to be added into the listeners vector & receive the callback. For example following method should be "add-able" into AddListener.
void ListenerMethod_Instance_3(int integer_data) {
std::cout << "ListenerMethod_Instance_3 called with integer_data " << integer_data << "\n";
}
Looking at this link here looks somewhat possible someway. But I'm struggling to adapt it to my use-case here. Please suggest.
So, basically how can I achieve templates functionality with std::functions ?
struct anything_view_t {
void* ptr=0;
template<class T, std::enable_if_t<!std::is_same<anything_view_t, std::decay_t<T>>{}, int> =0>
anything_view_t(T&&t):ptr(std::addressof(t)){}
anything_view_t()=default;
anything_view_t(anything_view_t const&)=default;
anything_view_t& operator=(anything_view_t const&)=default;
template<class T>
operator T() const { return *static_cast<T*>(ptr); }
};
this is a very unsafe type erasing view of anything.
struct any_callbacks {
std::unordered_map<std::type_index, std::vector<std::function<void(anything_view_t)>>> table;
template<class T>
void add_callback( std::function<void(T)> f ){
table[typeid(T)].push_back(f);
}
template<class T>
void invoke_callbacks(T t) const {
auto it = table.find(typeid(T));
if (it==table.end()) return;
for(auto&&f:it->second)
f(t);
}
};
something like the above should work. The type T must match exactly. References not supported. Code not compiled, design is sound, probably has typos.
This is not restructed to primitive types. You should pass T explicitly, don't rely on deduction as that is fragile.

How can you bind a variadic member function to a functor?

I am attempting to bind the first parameter of a variadic function using std::bind and then pass the returned functor to the connect() function of a boost::signals2::signal. The process works fine as long as the variadic function is not a member function. This is what I would like to be able to do:
class test {
public:
test() {}
void call_var_callback(string const& func, ...) {
va_list args;
va_start(args, func);
std::cout << "Calling variadic function: " << func << std::endl;
}
};
test t;
void register_callback2(std::map<string, boost::any>& sig_table,
string func, string event) {
auto event_entry = sig_table.find(event);
if (event_entry != sig_table.end()) {
if (event == "event1") {
auto sig = boost::any_cast<signal<void (void)>*>(sig_table["event1"]);
sig->connect(std::bind(&test::call_var_callback, &t, std::cref(func)));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
} else {
std::cout << "No such event exists!" << std::endl;
}
}
int main( void ) {
// define events
signal<void (void)> event1;
signal<void (char const*)> event2;
signal<void (int, char const*)> event3;
// intialize event / signal lookup table
std::map<string, boost::any> sig_tbl;
sig_tbl["event1"] = &event1;
sig_tbl["event2"] = &event2;
sig_tbl["event3"] = &event3;
// connect the signals
register_callback2(sig_tbl, "func1", "event1");
register_callback2(sig_tbl, "func2", "event2");
register_callback2(sig_tbl, "func3", "event3");
// call the signals
for (int i = 1000; i > 0; --i) {
(*boost::any_cast<signal<void (void)>*>(sig_tbl["event1"]))();
(*boost::any_cast<signal<void (char const*)>*>(sig_tbl["event2"]))("0xBAAD");
(*boost::any_cast<signal<void (int, char const*)>*>(sig_tbl["event3"]))(5, "0xBEEF");
}
}
When I compile I get an error that states there is "no match for call to ..." Where ... is the filled in templated type of the call to bind. If I move the definition of call_var_callback() outside the scope of the 'test' object, everything works. However, in my real code base I need the bound function to be a member of a class because it carries state along with it.
Thank you in advance for your consideration and assistance.
When you bind member functions, you don't take the address of the object. In your code :
sig->connect(std::bind(&test::call_var_callback, &t, std::cref(func)));
the &t is wrong, it should be a plain t
bind supports 2 main ways of binding the object: You can
pass an object itself or
a pointer to object (as well as a shared pointer instead of a raw pointer)

What is the syntax for storing a specific class's member functions in a vector?

I did quite a bit of searching, but the combination of * () and class scope has greatly hindered me in understanding of the syntax, with each edit throwing a new error, any help guys?
What I'm trying to do:
Declare a std::vector of pointers to member functions found in MyClass.h
Assign the actual member functions to the std::vector in MyClass.cpp's constructor
The member functions are not static
Thanks!
I'm curious where you're going to use them from. You see in order to call a C++ class member function you need to have an instance pointer with which to call it (each member function needs a this in order to access the class state). So normally you'd wrap the member function pointer together with the instance pointer with std::bind and then maybe store the result in std::function. To put them in vector they're all going to need the same signature.
Is this the kind of thing you were looking for:
class P
{
typedef std::function<void (void)> func_t;
std::vector<func_t> functions;
public:
P()
{
functions.push_back(std::bind(&P::foo1, this));
functions.push_back(std::bind(&P::foo2, this));
functions.push_back(std::bind(&P::foo3, this));
}
void foo1(void)
{
std::cout << "foo1\n";
}
void foo2(void)
{
std::cout << "foo2\n";
}
void foo3(void)
{
std::cout << "foo3\n";
}
void call()
{
for(auto it = functions.begin(); it != functions.end(); ++it)
{
(*it)();
}
}
};
int main()
{
P p;
p.call();
}
After further clarification from the OP I'll propose this:
class P
{
typedef std::function<void (void)> func_t;
std::map<const char*, func_t> functions;
public:
P()
{
functions["foo1"] = std::bind(&P::foo1, this);
functions["foo2"] = std::bind(&P::foo2, this);
functions["foo3"] = std::bind(&P::foo3, this);
}
void foo1(void)
{
std::cout << "foo1\n";
}
void foo2(void)
{
std::cout << "foo2\n";
}
void foo3(void)
{
std::cout << "foo3\n";
}
void call_by_name(const char* func_name)
{
functions[func_name]();
}
};
int main()
{
P p;
p.call_by_name("foo1");
p.call_by_name("foo2");
p.call_by_name("foo3");
}
You can use member function pointers like this (the C++11 is unrelated to that part):
struct S {
int foo(){std::cout<<"foo"; return 0;}
int bar(){std::cout<<"bar"; return 0;}
};
int main() {
std::vector<int(S::*)()> funcs{&S::foo, &S::bar};
S s;
for (auto func : funcs) {
(s.*func)();
}
}
However, if you use C++11, std::function can make it a bit cleaner:
std::vector<std::function<int(S &)>> funcs{&S::foo, &S::bar};
S s;
for (auto func : funcs) {
func(s);
}
If you use C++03, Boost has boost::function, which is similar.