模板函数作为模板参数 [英] Template function as a template argument

查看:404
本文介绍了模板函数作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚困惑了如何在C ++中以通用的方式实现某些东西。

I've just got confused how to implement something in a generic way in C++. It's a bit convoluted, so let me explain step by step.

考虑这样的代码:

void a(int) {
    // do something
}
void b(int) {
    // something else
}


void function1() {
    a(123);
    a(456);
}
void function2() {
    b(123);
    b(456);
}

void test() {
    function1();
    function2();
}

很容易注意到 function1 function2 做同样的事情,只有不同的部分是内部函数。

It's easily noticable that function1 and function2 do the same, with the only different part being the internal function.

使函数通用以避免代码冗余。我可以使用函数指针或模板。让我现在选择后者。 我的想法是,它是更好的,因为编译器一定能够内联函数 - 我是正确的吗?如果通过函数指针进行调用,编译器是否仍然内联调用?这是一个侧面问题。

Therefore, I want to make function generic to avoid code redundancy. I can do it using function pointers or templates. Let me choose the latter for now. My thinking is that it's better since the compiler will surely be able to inline the functions - am I correct? Can compilers still inline the calls if they are made via function pointers? This is a side-question.

确定,返回原点...包含模板的解决方案:

OK, back to the original point... A solution with templates:

void a(int) {
    // do something
}
void b(int) {
    // something else
}

template<void (*param)(int) >
void function() {
    param(123);
    param(456);
}

void test() {
    function<a>();
    function<b>();
}

一切OK。但是我遇到了一个问题:如果 a b p>

All OK. But I'm running into a problem: Can I still do that if a and b are generics themselves?

template<typename T>
void a(T t) {
   // do something
}

template<typename T>
void b(T t) {
   // something else
}

template< ...param... > // ???
void function() {
    param<SomeType>(someobj);
    param<AnotherType>(someotherobj);
}

void test() {
    function<a>();
    function<b>();
}



我知道模板参数可以是以下之一:

I know that a template parameter can be one of:


  • 一个类型,

  • 一个模板类型,

  • 类型。

这些都似乎没有覆盖我的情况。因此,我的主要问题是:我如何解决,即在上一个例子中定义 function()

None of those seems to cover my situation. My main question is hence: How do I solve that, i.e. define function() in the last example?

(是的,函数指针似乎是这种情况下的一个解决方法 - 如果他们也可以内联 - 但我正在寻找这类问题的一般解决方案)。

(Yes, function pointers seem to be a workaround in this exact case - provided they can also be inlined - but I'm looking for a general solution for this class of problems).

推荐答案

为了解决模板的这个问题,你必须使用模板模板参数。
不幸的是,你不能将模板模板函数作为类型传递,因为它必须首先被实例化。但是有一个虚拟结构的解决方法。这里是一个例子:

In order to solve this problem with templates, you have to use a template template parameter. Unfortunately, you cannot pass template template function as a type, because it has to be instantiated first. But there is a workaround with dummy structures. Here is an example:

template <typename T>
struct a {

    static void foo (T = T ())
    {
    }

};

template <typename T>
struct b {

    static void foo (T = T ())
    {
    }

};

struct SomeObj {};
struct SomeOtherObj {};

template <template <typename P> class T>
void function ()
{
    T<SomeObj>::foo ();
    T<SomeOtherObj>::foo ();
}

int main ()
{
    function<a>();
    function<b>();
}

这篇关于模板函数作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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