Short version:
Instead of always having to type:
auto function_pointer = &decorator<int, Foo, void &Foo::bar(int)>
I would like to be able to just write
auto function_pointer = &decorator<void &Foo::bar(int)>
where the int
and Foo
are automatically extracted from <void &Foo::bar(int)
>.
For starters I have:
map<string, Object*> all_object_instances;
class Object {
public:
Object(const string &name) { all_object_instances[name]=this; }
virtual ~Object() { all_object_instances.erase(name); }
};
class Foo : public Object {
public:
Foo(const string &name) : Object(name) {}
void bar(int);
};
I need a function, that will call Foo::bar(int) with some decoration, so I write:
template <class Arg, class C, void C::*T(Arg)>
void decorator(const string &name, const string &s_arg)
{
Arg a = my_convert(s_arg);
C* c = dynamic_cast<C*>(all_object_instances[name]);
(c->*T)(a);
}
So my main code has to look like this:
new Foo("MyFoo");
....
auto saved_f = &decorator<int, Foo, void &Foo::bar(int)>;
....
saved_f("MyFoo", "123");
....
delete all_object_instances("MyFoo") // for symmetry
It would be a lot better, if I could just have 1 template argument:
saved_f = &decorator<void &Foo::bar(int)>;
And derive both 'Foo', and 'int' from the argument:
template <TEMPLATE MAGIC>
void decorator(const string &name, const string &s_arg)
{
typedef ARG_MAGIC ARG;
typedef CLASS_MAGIC C;
Arg a = my_convert(s_arg);
C* c = dynamic_cast<C*>(all_object_instances[name]);
(c->*T)(a);
}
Does any such thing exist?