嵌套C++函数模板和typename模板

huangapple go评论60阅读模式
英文:

nesting c++ functions template and typename template

问题

我正在尝试在C++17中嵌套模板,其中我想要对函数进行模板化,同时还要模板化这些函数可以接受的参数。

这是我尝试做的事情的一个最小复现示例。

首先,我创建了一个函数模板,我已经成功完成了这一部分。

double add(double x, double y) { return x + y; }
double diff(double x, double y) { return x - y; }

template <double fn1(double, double), double fn2(double, double)>
double combine(double x, double y) {
    return fn1(x, y) + fn2(x, y);
}

我可以这样使用它:

auto z = combine<add, diff>(x, y);

现在,我还想使用模板来支持传递的函数中的不同浮点精度。所以首先我创建了一个关于add和diff函数的模板:

template <typename T>
T add(T x, T y) { return x + y; }
T diff(T x, T y) { return x - y; }

但然后我陷入了困境。如何修改combine函数才能正常工作呢?我下面的第一个想法不起作用:

template <typename T, T fn1(T, T), T fn2(T, T)>
T combine(T x, T y) {
    return fn1(x, y) + fn2(x, y);
}
英文:

I am trying to nest template in c++17 where I want to template on functions but also on the arguments that these function can accept.

here is a minimal reproducing example of what I am trying to do.

At first I make a template on function. I have managed to do this part.

double add(double x, double y){return x+ y;}
double diff(double x, double y){return x - y;}

template &lt;double fn1(double, double), double fn2(double, double)&gt;
double combine(double x, double y) {

    return fn1(x, y) + fn2(x, y);
}

I manage to use it this way:

auto z = combine&lt;add, diff&gt;(x, y);

Now I would like to also use template to support difference floating point precision in the functions that I am passing.
So first I create a template on the add and diff functions

template &lt;typename T&gt;
T add(T x, T y){return x+ y;}
T diff(T x, T y){return x - y;}

But then I am stuck. How to I modify the combine function to work?
The first idea I had bellow does not work

template &lt;typename T,T fn1(T, T), T fn2(T, T)&gt;
T combine(T x, T y) {

    return fn1(x, y) + fn2(x, y);
}

答案1

得分: 0

以下是已翻译的内容:

here is a minimal reproducing example of what I am trying to do

这里有一个我尝试做的最小复制示例

Well, not quite.

嗯,并不完全是这样。

There's a missing template &lt;typename T&gt; before T diff(T x, T y){return x - y;} and it's not shown how the templated combine is called. If you try to use the same

T diff(T x, T y){return x - y;}之前缺少了template &lt;typename T&gt;,并且没有显示出如何调用模板化的combine函数。如果你尝试使用相同的

auto z = combine&lt;add, diff&gt;(x, y);

The template parameter T can't be deduced.

模板参数T无法被推断出。

You can either pass all the template parameters when calling combine:

在调用combine时,你可以传递所有模板参数:

float x = combine&lt;float, add, diff&gt;(1.0f, 3.14f); // -&gt; 2

Or use an alias

或者使用别名

auto&amp; z = combine&lt;double, add, diff&gt;;

float x = z(3.5, 5.17); // -&gt; 7

Se e.g.: https://godbolt.org/z/b1ETqbj9c

示例参见:https://godbolt.org/z/b1ETqbj9c

英文:

> here is a minimal reproducing example of what I am trying to do

Well, not quite.

There's a missing template &lt;typename T&gt; before T diff(T x, T y){return x - y;} and it's not shown how the templated combine is called. If you try to use the same

auto z = combine&lt;add, diff&gt;(x, y);

The template parameter T can't be deduced.

You can either pass all the template parameters when calling combine:

float x = combine&lt;float, add, diff&gt;(1.0f, 3.14f); // -&gt; 2

Or use an alias

auto&amp; z = combine&lt;double, add, diff&gt;;

float x = z(3.5, 5.17); // -&gt; 7

Se e.g.: https://godbolt.org/z/b1ETqbj9c

huangapple
  • 本文由 发表于 2023年4月4日 14:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定