将指向模板函数的指针作为函数参数传递?
假设我想要一个 C++ 函数对两个输入执行算术,将它们视为给定类型:
伪:
function(var X,var Y,function OP)
{
if(something)
return OP<int>(X,Y);
else if(something else)
return OP<double>(X,Y);
else
return OP<string>(X,Y);
}
适合 OP 的函数可能如下所示:
template <class T> add(var X,var Y)
{
return (T)X + (T)Y; //X, Y are of a type with overloaded operators
}
那么,问题是函数的签名是什么样的? 如果运算符函数不是模板化的,我可以做到,但我对这种额外的复杂性感到困惑。
Say I want a C++ function to perform arithmetic on two inputs, treating them as a given type:
pseudo:
function(var X,var Y,function OP)
{
if(something)
return OP<int>(X,Y);
else if(something else)
return OP<double>(X,Y);
else
return OP<string>(X,Y);
}
functions that fit OP might be like:
template <class T> add(var X,var Y)
{
return (T)X + (T)Y; //X, Y are of a type with overloaded operators
}
So, the question is what would the signature for function look like? If the operator functions are non-templated I can do it, but I get confused with this extra complexity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
模板函数不能作为模板参数传递。 在将此函数传递给另一个模板函数之前,您必须手动推导该函数的模板参数。 例如,你有一个函数,
你想将它传递给callFunc:
你不能简单地写
你必须写
为了能够传递sum而不写int,你有编写一个函子 - 具有将调用模板函数的operator() 的结构或类。 然后您可以将此函子作为模板参数传递。 这是一个例子。
Template functions cannot be passed as template arguments. You have to manually deduce template arguments for this function before you pass it to another template function. For example, you have function
You want to pass it to callFunc:
You can't simply write
You have to write
To be able to pass sum without writing int, you have to write a functor - struct or class with operator() that will call your template function. Then you can pass this functor as template argument. Here is an example.
您在找这个吗?
或者您正在寻找类似 add 之类的东西?
通过以下方式调用:
Are you looking for this?
Or are you looking for something that calls something like add?
Called via:
我有点困惑……为什么你的伪代码中存在类型差异?
C++ 模板允许对模板进行完全类型推导:
这里,
F
适合任何可以使用()
函数调用语法调用的任何内容(尤其是函数),并且只接受两个类型的参数T
(或隐式转换为它)。I'm a bit confused … why the type differentiation in your pseudo-code?
C++ templates allow full type deduction on templates:
Here,
F
fits anything (especially functions) that may be called with the()
function call syntax and accepting exactly two arguments of typeT
(or implicitly convertible to it).我为此使用 lambda。
I use lambdas for this.
我认为您正在寻找策略模式。
I think you're looking for the Strategy Pattern.
我不确定你问题中的这个
var
是什么意思。 它肯定不是一个有效的 C++ 关键字,因此我假设它是类似于boost:any
的类型。 此外,该函数缺少结果类型。 我添加了另一个var
,无论它是什么。 您的解决方案可能如下所示:有趣的模板参数本身就是模板,因此其名称为“模板模板参数”。 您传入模板的名称,而不是实例的名称。 也就是说,您传递的是
std::plus
,而不是std::plus
:I'm not sure what this
var
thing in your question means. It's certainly not a valid C++ keyword, so I assume it's a type akin toboost:any
. Also, the function is missing a result type. I added anothervar
, whatever that might be. The your solution could look like this:The funny template argument is a template itself, hence its name "template template argument". You pass in the name of a template, not an instance. That is, you pass
std::plus
, notstd::plus<int>
: