Ale*_*cki 6 c++ templates c++11
有没有办法可以从表单中的函数签名中提取类型,foo(bar)并获得对just foo或bar.所以,如果我有模板:
template<typename signiture>
class type{
};
Run Code Online (Sandbox Code Playgroud)
签名是什么foo(bar),然后在读取的类中有一个函数
foo function(bar b){
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
我正在接口std::function并发现使用foo(bar)语法更方便,而不是使用多个模板参数,如下所示:
template<typename return_t,param_tps... args>
class type{
return_t function(param_ps args...){
return something;
}
};
Run Code Online (Sandbox Code Playgroud)
如果我能澄清请告诉我?提前致谢.
编辑:为了澄清我感兴趣的是一个具有N个参数的函数,这个参数由特定的类实例指定的任何东西来确定.
编辑2:此问题所依据的代码如下:
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;
};
Run Code Online (Sandbox Code Playgroud)
它是Observer模式的一种实现,它是非虚拟的,std::function用于将两者之间的东西粘合在一起.我想使用foo(bar)语法,因为它更有利于类的易用性.问题是将函数类型签名转换为返回类型和参数类型,以便operator()在主题类中指定正确的,以便它可以使用正确的数据通知观察者.
所做的更改基于以下给出的示例,如下所示:
template<typename t>struct type;
template<typename R,typename... args_t>
struct type<R(args_t...)>{
//use R and args_t as needed
}
Run Code Online (Sandbox Code Playgroud)
感谢所有帮助过的人.
这是一个非常基本的解决方案,适用于接受一个参数的函数(似乎你在这个问题中放置了这个约束,但是一个通用的解决方案很容易提供,如下所示):
template<typename S>
struct type; // You can leave this undefined, because the template is
// supposed to be instantiated with a function type, and
// that is matched by the specialization below.
template<typename R, typename Arg>
struct type<R(Arg)>
{
// Just use R and Args as you wish here..
};
Run Code Online (Sandbox Code Playgroud)
这是一个可能的例子(Coliru的现场演示):
#include <type_traits>
template<typename S>
struct signature;
template<typename R, typename Arg>
struct signature<R(Arg)>
{
using return_type = R;
using argument_type = Arg;
};
int main()
{
using ret = signature<void(int)>::return_type;
using arg = signature<void(int)>::argument_type;
static_assert(std::is_same<ret, void>{}, "!");
static_assert(std::is_same<arg, int>{}, "!");
}
Run Code Online (Sandbox Code Playgroud)
如果您对可变参数的更通用的解决方案感兴趣,这可能是您正在寻找的:
#include <tuple>
struct type; // You can leave this undefined, because the template is
// supposed to be instantiated with a function type, and
// that is matched by the specialization below.
template<typename R, typename... Args>
struct type<R(Args...)>
{
// Just use R and Args... as you with here..
};
Run Code Online (Sandbox Code Playgroud)
这是一个可能的使用示例(Coliru上的现场演示):
#include <tuple>
#include <type_traits>
template<typename S>
struct signature;
template<typename R, typename... Args>
struct signature<R(Args...)>
{
using return_type = R;
using argument_type = std::tuple<Args...>;
};
int main()
{
using ret = signature<void(int, double)>::return_type;
using arg1 = std::tuple_element_t<0, signature<void(int, double)>::argument_type>;
using arg2 = std::tuple_element_t<1, signature<void(int, double)>::argument_type>;
static_assert(std::is_same<ret, void>{}, "!");
static_assert(std::is_same<arg1, int>{}, "!");
static_assert(std::is_same<arg2, double>{}, "!");
}
Run Code Online (Sandbox Code Playgroud)