模板专门化函数的参数 [英] template to specialize a function's parameter

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

问题描述

我有一个函数 int f(int x,int y)需要调用自己很多次,其中一个参数固定,如

I have a function int f (int x, int y) which needs to call itself plenty of times, with one of its parameters fixed, as in

int f(int x, int y) { 
      ...
      int i = f(z,y);
      ...
}

有没有通过模板定义的方法一个函数 int g(int x)使得 g(z):= f(z,y)上述调用将是 int i = g(z)

Is there any way of defining via a template a function int g (int x) such that g(z) := f(z,y) so that the above call would have been int i = g(z)?

推荐答案



You can just define that without any templating,

auto f( int x, int y )
    -> int
{
    auto g = [=]( int z ) -> int { return f( z, y ); };
    // ...
    int i = g( z );
}

您可以省略 - >

You can omit the -> int result type specification for g if you want.

免责声明:代码没有被触及。

Disclaimer: code not touched by compiler's hands.

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

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