有没有一种方法可以将模板化的函数签名作为模板模板参数传递
Is there a way to pass templated function signature as a template template parameter
通过使用模板模板参数,可以将模板化类传递给类,而无需在其参数上指定类型。我想知道是否有一种方法可以将函数的模板化签名传递到模板模板参数中,以便能够专门化要向前考虑的函数变体。
需要明确的是,我知道我不能这么做:
template <class T>
void foo() { /*...*/ }
template <template <class...> class FooType>
struct Foo { /*...*/ };
int main() {
Foo<decltype(foo)> f;
}
但不知何故,我希望能够将函数的模板签名传递给Foo
。这可能吗?
我不敢相信这是不可能的,所以我搜索了一下,找到了一种方法来做我想要的事情。我使用了模板using
,语法为:
template <template<class... Args> class FooType>
struct Foo {
FooType<int> ft;
};
template <class Res, class... Args>
using FooSignature = Res(*)(Args...);
int foo() {
return 1;
}
int main() {
Foo<FooSignature> f;
f.ft = foo;
}
然而,这仍然留下了一个问题,因为标准中有相反的规定,这怎么可能呢。
在下面的例子中,有一个模板模板参数,它接受函数的首选签名
由于模板类的特殊化和缺少主体,因此只接受可调用的类型
这是OP实际要求的概括:
#include<cassert>
template<typename F>
struct S;
template<typename R, typename... Args>
struct S<R(Args...)> {
using type = R(*)(Args...);
};
template<template<typename> class F>
struct T {
typename F<void(int)>::type ft;
typename F<double(double, double)>::type gt;
};
void f(int) { }
double g(double x, double y) { return x+y; }
int main() {
T<S> t;
t.ft = f;
t.gt = g;
t.ft(42);
auto v = t.gt(1., 1.);
assert(v == 2.);
}
从这个答案中可以看出
函数指针的模板在C++中是非法的
C++标准表示,为14/1美元
模板定义类或函数族。
进一步引用链接答案:
请注意,它并没有说"模板定义了一系列类、函数或函数指针"
然而,您可以传递具体的函数指针,并专门研究它们的签名:
#include <iostream>
template <class T>
void foo(T) { }
template <typename>
struct Foo;
template<typename T>
struct Foo<void(T)>
{
void cb() { std::cout << "Tn"; }
};
template<>
struct Foo<void(int)>
{
void cb() { std::cout << "intn"; }
};
template<>
struct Foo<void(double)>
{
void cb() { std::cout << "doublen"; }
};
int main()
{
Foo<decltype(foo<int >)>().cb(); // outputs 'int'
Foo<decltype(foo<double>)>().cb(); // outputs 'double'
Foo<decltype(foo<char >)>().cb(); // outputs 'T'
return 0;
}
template的模板仍然是一个模板。
template <class T>
void foo() { /*...*/ }
template <typename T>
struct Foo { /*...*/ };
int main() {
Foo<decltype(foo<int>)> f;
}
不能将函数模板作为参数传递。可以做的是将函数模板包装在生成lambda中,并使用标记参数:
template <class T> struct tag_t { using type = T; };
template <class T>
void foo() { ... }
template <class F>
void call_func_with(F f) {
f(tag_t<int>{} );
f(tag_t<double>{} );
}
call_with_func([](auto tag) { foo<decltype(tag)::type>(); } );
这里,f(tag_t<X>{} )
根据需要最终调用foo<X>()
。
相关文章:
- 如何在C++中将迭代器作为函数参数传递
- 为什么我不能将引用作为 std::async 的函数参数传递
- 节点插件 API 将数组作为函数参数传递
- 将字符串数组作为函数参数传递
- 如何通过 C++ 中的函数参数传递初始值设定项列表的固定大小的初始值设定项列表?
- 我可以动态创建新地图并作为函数参数传递吗?
- 将任意模板向量作为函数参数C++传递
- 将函数指针作为函数参数传递的目的
- C++:通过函数参数传递的值给出不同的结果
- 当数组大小在C++中未知时,如何在运行时将对字符串数组的引用作为函数参数传递?
- 通过宏将函数参数传递给函数
- C++:访问作为函数参数传递的双指针数组所指向的变量的值
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 将派生类构造函数参数传递给受保护的成员
- 将指向数组的指针作为函数参数传递,这本身就是另一个函数的返回值?
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 有什么理由C++ 11+ std::mutex 应该声明为全局变量,而不是作为函数参数传递到 std::thread 中
- 如何将派生类作为函数参数传递:如果派生类成员不在父类中怎么办
- 如何将变量作为构造函数参数或函数参数传递
- 当数组对象以函数参数传递时,为什么复制构造函数会自称