将函数参数的参数解包到C ++模板类 [英] Unpacking arguments of a functional parameter to a C++ template class
问题描述
我有一个问题涉及C ++中模板类的函数模板参数。
I have a question involving functional template arguments to template classes in C++.
我想定义一个模板类 Foo
使用单个模板参数
Fun
template <typename Fun>
struct Foo {
...
};
,使得给定像
void bar(std::string a, float b, char c)
{
...
}
然后 Foo< bar> :: args_t
将等同于
std::tuple<std::string, float, char>
这可能吗? (使用 std :: tuple
这里只是为了具体性。更多一般我想知道是否可以做一些功能模板的参数上的模式匹配参数。)
Is this possible? (The use of std::tuple
here is just for concreteness. More generally I'm wondering if it's possible to do something like pattern-matching on the arguments of a functional template parameter.)
这一点是为了避免以
template Foo<typename A, typename B, typename C, typename D,
D (*Fun)(A a, B b, C c)>
struct Foo {
typedef std::tuple<A,B,C> args_t;
};
这需要提交固定数量的函数参数,并且需要参数和返回类型函数作为模板参数显式提供。 (定义 Foo
使用可变参数模板可能会解决前一个问题,但后者是什么?)
which requires both committing to a fixed number of function arguments, and requiring the argument and return types of the function to be provided explicitly as template parameters. (Defining Foo
using variadic templates could presumably solve the former issue, but what about the latter?)
推荐答案
声明主模板并保留未实现。
Declare a primary template and leave it unimplemented.
template<typename T>
struct foo; // unimplemented primary template
然后提供与模板参数匹配的部分特化。 p>
Then provide a partial specialization that matches function types as the template argument.
template<typename Result, typename... Args>
struct foo<Result(Args...)>
{
using args_t = std::tuple<Args...>;
};
您可以访问嵌套类型
foo<decltype(bar)>::args_t
这篇关于将函数参数的参数解包到C ++模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!