有没有一种简洁的方法可以在不使用宏的情况下指向模板函数的所有实例?
我有几个模板化函数,我想测试各种类型:
template<typename T>
void function1() {
return;
}
template<typename T>
void function2() {
return;
}
template<typename T>
void function3() {
return;
}
我可以用一个宏来做到这一点:
#define TEST_ACROSS_TYPES(fn) \
fn<int>(); \
fn<bool>(); \
fn<char>(); \
fn<double>(); \
TEST_ACROSS_TYPES(function1);
TEST_ACROSS_TYPES(function2);
但是,(1) 宏很难看并且其他人难以遵循,并且 (2) 我正在使用 CATCH
,这在使用宏设置测试用例时效果不佳。
有没有办法做这样的事情:
void testAcrossTypes(SomeType f) {
f<int> ();
f<bool> ();
f<char> ();
f<double> ();
}
除了定义 SomeType
的问题外,这看起来更简洁了。这个问题(How to define typedef of function pointer which has template arguments)解释了如何定义一个指向模板函数的指针;但是,需要指定模板参数。
澄清一下:假设 function1
、function2
和 function3
分别测试不同的模板化函数。每个函数都需要测试int
、byte
、char
、double
等,我想避免为每个函数显式设置许多(即 num_functions * num_types)测试。相反,我想要一个指向测试函数(function1
、function2
等)并为每个模板类型运行它的方法,从而巩固
function1<int>();
function1<byte>();
function1<char>();
function1<double();
...
function2<int>();
function2<byte>();
function2<char>();
function2<double();
...
function3<int>();
function3<byte>();
function3<char>();
function3<double();
...
每个测试函数只调用一次
testAcrossTypes(function1);
testAcrossTypes(function2);
testAcrossTypes(function3);
最佳答案
你想用什么来完成
void testAcrossTypes(SomeType f) {
f<int> ();
f<bool> ();
f<char> ();
f<double> ();
}
如果 SomeType
可以是模板模板参数,那将是可能的。但是,该标准不允许函数模板作为模板模板参数。
来自 C++11 标准:
14.3.3 Template template arguments
1 A template-argument for a template template-parameter shall be the name of a class template or an alias template, expressed as id-expression.
最好的选择是使用仿函数而不是函数。示例:
template<typename T>
struct function1
{
void operator()() {
return;
}
};
template<typename T>
struct function2
{
void operator()() {
return;
}
};
template < template <typename> class F>
void testAcrossTypes() {
F<int>()();
F<bool>()();
F<char>()();
F<double>()();
}
int main()
{
testAcrossTypes<function1>();
testAcrossTypes<function2>();
}
关于所有模板实例的 C++ 单一函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38494276/