英文:
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 <double fn1(double, double), double fn2(double, double)>
double combine(double x, double y) {
return fn1(x, y) + fn2(x, y);
}
I manage to use it this way:
auto z = combine<add, diff>(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 <typename T>
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 <typename T,T fn1(T, T), T fn2(T, T)>
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 <typename T>
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 <typename T>
,并且没有显示出如何调用模板化的combine
函数。如果你尝试使用相同的
auto z = combine<add, diff>(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<float, add, diff>(1.0f, 3.14f); // -> 2
Or use an alias
或者使用别名
auto& z = combine<double, add, diff>;
float x = z(3.5, 5.17); // -> 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 <typename T>
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<add, diff>(x, y);
The template parameter T
can't be deduced.
You can either pass all the template parameters when calling combine:
float x = combine<float, add, diff>(1.0f, 3.14f); // -> 2
Or use an alias
auto& z = combine<double, add, diff>;
float x = z(3.5, 5.17); // -> 7
Se e.g.: https://godbolt.org/z/b1ETqbj9c
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论