C ++所有模板实例的单个函数指针 [英] C++ Single function pointer for all template instances

查看:151
本文介绍了C ++所有模板实例的单个函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法指向模板函数的所有实例而不使用宏?

Is there a concise way to point to all instances of a templated function without using macros?

我有几个模板函数,类型:

I have several templated functions that I want to test across a variety of types:

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)宏是丑陋和难以让别人遵循, m使用 CATCH ,这在使用宏设置测试用例时不起作用。

But, (1) Macros are ugly and hard for others to follow, and (2) I'm using CATCH, which doesn't play nice when using macros to set up test cases.

可以这样做:

void testAcrossTypes(SomeType f) {
  f<int> ();
  f<bool> ();
  f<char> ();
  f<double> ();
}

这看起来更清洁,除了定义 SomeType 。此问题(如何定义函数指针的typedef)它有模板参数)解释了如何定义一个指向模板函数的指针;但是,要求指定模板参数。

which seems much cleaner, except for the problem of defining SomeType. This question (How to define typedef of function pointer which has template arguments) explains how to define a pointer to a templated function; but, requires that the template arguments be specified.

澄清:Imagine function1 function2 function3 每个测试不同的模板函数。每个函数需要测试 int byte char double 等。我想避免为每个函数显式地设置很多(即num_functions * num_types)个测试。相反,我想有一个方法指向测试函数( function1 function2 等)和为每个模板类型运行它,从而合并

For clarification: Imagine function1, function2, and function3 each test a different templated function. Each function needs to be tested for int, byte, char, double, etc. I want to avoid having to explicitly set up many (i.e. num_functions * num_types) tests for each function. Instead, I want to have a single method that points to the test function (function1, function2, etc.) and runs it for each template type, thus consolidating

function1<int>();
function1<byte>();
function1<char>();
function1<double();
...
function2<int>();
function2<byte>();
function2<char>();
function2<double();
...
function3<int>();
function3<byte>();
function3<char>();
function3<double();
...

每个测试函数只能调用一次

into just one call per test function

testAcrossTypes(function1);
testAcrossTypes(function2);
testAcrossTypes(function3);


推荐答案



What you are trying to accomplish with

void testAcrossTypes(SomeType f) {
  f<int> ();
  f<bool> ();
  f<char> ();
  f<double> ();
}

如果 SomeType 可以是模板模板参数。但是,标准不允许函数模板作为模板模板参数。

would be possible if SomeType could be a template template argument. However, the standard does not allow function templates as template template argument.

从C ++ 11标准:

From the C++11 Standard:

14.3.3模板模板参数

1模板模板参数必须是类模板或别名模板的名称,以id-expression表示。

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.

是使用函子而不是函数。示例:

Your best option is to use functors instead of functions. Example:

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 ++所有模板实例的单个函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆