简体   繁体   English

用于专门化函数参数的模板

[英]template to specialize a function's parameter

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 f(int x, int y) { 
      ...
      int i = f(z,y);
      ...
}

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) ? 有没有办法通过模板定义一个函数int g (int x) ,使得g(z) := f(z,y)以便上面的调用是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. 如果需要,可以省略g-> int结果类型规范。

Disclaimer: code not touched by compiler's hands. 免责声明:编译器未触及的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM