是否有一种干净的方法将模板参数转发到模板函数?
但是,以下作用是
#include <iostream>
template<typename A, typename B> double func1(B x, B y)
{
A a = 3.0f;
return a + x + y;
}
template<typename A, typename B> double func2()
{
B x = 5.0;
B y = 8.0;
return func1<float>(x, y);
}
int main()
{
std::cout << func2<float, double>() << "\n";
}
,如果我想用另一个类似功能替换 func1
该怎么办?一个具有相同数字和类型的参数和模板参数。我尝试了模板&lt; template&lt; ...
如下所示,但似乎没有用。
#include <iostream>
template<typename A, typename B> double func1(B x, B y)
{
A a = 3.0f;
return a + x + y;
}
template<template <typename, typename> typename func, class A, class B>
double func2()
{
B x = 5.0;
B y = 8.0;
return func<A, B>(x, y);
}
int main()
{
std::cout << func2<func1, float, double>() << "\n";
}
如果上面的模式有效,那就太好了。
如果 func
可以部分地进行模板,并且仍然从参数的类型中推断出更好的说法,例如
#include <iostream>
template<typename A, typename B> double func1(B x, B y)
{
A a = 3.0f;
return a + x + y;
}
template<template <typename, typename> typename func, class A, class B>
double func2()
{
B x = 5.0;
B y = 8.0;
return func<A>(x, y);
}
int main()
{
std::cout << func2<func1, float, double>() << "\n";
}
关于如何使第二片片段工作的任何想法,更好的是更好?
您还可以通过模板扣除来做到这一点吗? C ++ 20个概念可以在这里做魔术吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
功能模板具有一些类模板没有的限制。考虑
func1&lt; a,b&gt;
的类型对您无济于事。您需要调用具体功能。另一方面,使用类模板,您需要知道的所有类型(模板的实例化)就是使用它。如果您仅更改
func1
为函数,则可以使用operator()
:从参数中推论
b
,则 可以使用第二个代码段。可以使operator()
模板:Function templates have some restrictions that class templates do not have. Consider that the type of
func1<A,B>
alone does not help you. You'd need the concrete function to call it. With a class template on the other hand, the type (an instantiation of the template) is all you need to know to use it.Your 2nd code snippet works if you only change
func1
to be a functor, a class withoperator()
:To deduce
B
from the parameters you can makeoperator()
a template:替代其他答案中给出的模板函数,也可以使用通用lambda,以及 lambda的模板支持来自 c ++ 20 。
这需要打电话为:
请参阅实时演示
Alternative to the templated functors given in the other answers, one can also use a generic lambda, along with the lambda's template support from c++20.
This required the calling to be like:
See live demo
您可以将
func1
转换为带有模板operator()
的函数,而部分 通过<指定模板参数代码> .template operator()&lt; a&gt; 。You can turn
func1
into an functor with a templateoperator()
, and partially specify the template parameter through.template operator()<A>
.