将重载函数转换为模板函子 [英] Convert overloaded function to template functor
本文介绍了将重载函数转换为模板函子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些重载的函数,例如
int a(int){/*...*/}
float a(float){/*...*/}
/ * ... * /
int b(int){/*...*/}
float b(float){/*...*/}
/ * ... * /
$ b b
我的目标是将这些函数包装到函子对象中:
template< typename T&
struct func_a {
auto T operator()(T t) - > decltype(a(t)){return a(t);}
};
有一种方法来定义上面的模板结构在一些其他模板采用重载函数作为参数?类似这样:
模板< / *指向重载函数f * />
struct create_functor {
template< typename T>
struct func {
auto operator()() - > decltype(f(t)){return f(t);}
}
};因此,我可以在编译时生成结构体,如:
$ b typedef create_functor< a> :: func< int> func_a_int;
typedef create_functor< a> :: func< float> func_a_float;
typedef create_functor< b> :: func< int> func_a_int;
typedef create_functor< b> :: func< float> func_a_float;
解决方案您可以为每个函数定义一个重载集例如:
int a(int i){return 2 * i;}
float a return 3 * d;}
#define overload_set(f,f_set)\
struct f_set {\
template< typename ... Args> \
auto operator()(Args& ... args)\
- > decltype(f(std :: forward< Args>(args)...))\
{\
return f(std :: forward& \
} \
}
overload_set(a,a_set);
// more overload_set here ...
使用重载集而不是函数指针create_functor实现直接:
template< typename OverloadSet>
struct create_functor {
template< typename ... Args>
struct func {
auto operator()(Args ... args)
- > decltype(OverloadSet {}(args ...))
{
return OverloadSet {}(args ...);
}
};
};
create_functor< a_set> :: func< int> func_a_int; //注意:不需要typedef这里
create_functor< a_set> :: func< float> func_a_float;
int main()
{
std :: cout< func_a_int(2)<< std :: endl;
std :: cout<< func_a_float(3。)< std :: endl;
}
I have a few overloaded functions, e.g.
int a(int) {/*...*/}
float a(float) {/*...*/}
/* ... */
int b(int) {/*...*/}
float b(float) {/*...*/}
/* ... */
My goal is to wrap these function into a functor object:
template <typename T>
struct func_a {
auto T operator()(T t) -> decltype(a(t)) {return a(t);}
};
Is there a way to define the above template struct over some other template taking the overloaded function as argument? Something like this:
template </* pointer to an overloaded function f */>
struct create_functor {
template <typename T>
struct func {
auto operator()() -> decltype(f(t)) {return f(t);}
}
};
So I can generate the struct on compile-time, as in:
typedef create_functor<a>::func<int> func_a_int;
typedef create_functor<a>::func<float> func_a_float;
typedef create_functor<b>::func<int> func_a_int;
typedef create_functor<b>::func<float> func_a_float;
解决方案 You can define an overload set for each of your functions e.g :
int a(int i) {return 2*i;}
float a(float d) {return 3*d;}
#define overload_set(f, f_set) \
struct f_set { \
template <typename... Args> \
auto operator()(Args&&... args) \
-> decltype(f(std::forward<Args>(args)...)) \
{ \
return f(std::forward<Args>(args)...); \
} \
}
overload_set(a, a_set);
// more overload_set here...
Using overload set instead of function pointer makes the "create_functor" implementation straightforward :
template <typename OverloadSet>
struct create_functor {
template <typename... Args>
struct func {
auto operator()(Args... args)
-> decltype(OverloadSet{}(args...))
{
return OverloadSet{}(args...);
}
};
};
create_functor<a_set>::func<int> func_a_int; // Note: no need for typedef here
create_functor<a_set>::func<float> func_a_float;
int main()
{
std::cout << func_a_int(2) << std::endl;
std::cout << func_a_float(3.) << std::endl;
}
这篇关于将重载函数转换为模板函子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文