Is there a way that I can extract the types from a function signature in the form foo(bar)
and get access to just foo
or bar
. So if I have the template:
template<typename signiture>
class type{
};
where signiture is foo(bar)
and then have a function in the class that reads
foo function(bar b){
//do stuff
}
I am interfacing with std::function
and have found it more convenient to use the foo(bar)
syntax instead of using multiple template params like so:
template<typename return_t,param_tps... args>
class type{
return_t function(param_ps args...){
return something;
}
};
Let me know if i can clarify please? Thanks in Advance.
EDIT: for clarification i am interested in a function with N number of parameters to be determined by whatever the specific class instance specifies.
EDIT 2: The code that this question is based off of is as follows:
using std::function;
template <typename signiture>
class Observer;
template <typename return_t, typename ...args_t>
class Observer<return_t(args_t...)> {
protected:
using signature = return_t(args_t...);
typedef function<signature> func_t;
~Observer(){}
func_t what_to_do;
public:
Observer(Subject<signature>& subject,func_t what):what_to_do(what){
subject.Attach(what_to_do);
}
return_t operator()(args_t... args){
what_to_do(args...);
}
};
using std::function;
using std::vector;
template <typename signature>
class Subject;
template <typename return_t,typename...param_tps>
class Subject<return_t(param_tps...)> {
using signature=return_t(param_tps...);
public:
void Attach(std::function<signature> o){
obs.push_back(o);
}
void operator()(param_tps... params){
for (typename vector<std::function<signature>>::const_iterator i=obs.begin(); i!=obs.end(); ++i) {
(*i)(params...);
}
}
protected:
~Subject(){}
vector<std::function<signature>> obs;
};
It is an implementation of the Observer pattern that is non-virtual using std::function
to glue things together between the two. I wanted to use the foo(bar)
syntax because everything it is more conducive to the ease of use of the classes. The issue was converting the function type signature into the return type and the parameter types in order to specify the proper operator()
in the subject class so that it can notify the observers with the correct data.
The changes that were made were based upon the example given below as follows:
template<typename t>struct type;
template<typename R,typename... args_t>
struct type<R(args_t...)>{
//use R and args_t as needed
}
Thanks to all who helped.