c++ 函子和函数模板
考虑一下这个简单且毫无意义的代码。
#include <iostream>
struct A {
template<int N>
void test() {
std::cout << N << std::endl;
}
};
int main() {
A a;
a.test<1>();
}
这是一个非常简单的函数模板示例。 但是,如果我想将 A::test
替换为重载的 operator()
以使其成为函子,该怎么办?
#include <iostream>
struct A {
template<int N>
void operator()() {
std::cout << N << std::endl;
}
};
int main() {
A a;
a<1>(); // <-- error, how do I do this?
}
当然,如果operator()
采用依赖于模板的参数,编译器可能会推断出模板。 但我只是无法找出使用无参数函子指定模板参数的正确语法。
有正确的方法来做到这一点吗?
显然,这段代码可以工作,因为它绕过了函子语法:
a.operator()<1>();
但这有点违背了它作为函子的目的:-P。
consider this simple and pointless code.
#include <iostream>
struct A {
template<int N>
void test() {
std::cout << N << std::endl;
}
};
int main() {
A a;
a.test<1>();
}
It is a very simple example of a function template. What if however, I wanted to replace A::test
with an overloaded operator()
to make it a functor?
#include <iostream>
struct A {
template<int N>
void operator()() {
std::cout << N << std::endl;
}
};
int main() {
A a;
a<1>(); // <-- error, how do I do this?
}
Certainly if the operator()
took parameters which were dependent on the template, the compiler could possibly deduce the template. But I just can't figure out the proper syntax to specify template parameters with a parameterless functor.
Is there a proper way to do this?
Obviously, this code would work since it bypasses the functor syntax:
a.operator()<1>();
but that kinda defeats the purpose of it being a functor :-P.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您只能调用
,但不能使用函子。 函子需要一个非模板operator(),因为它们必须能够被调用为varname(),而这不适用于您的代码。
为了使其成为真正的函子,请将代码更改为模板类(函子是类):
You can only call
but that would not be using a functor. Functors need a non template operator(), as they must be able to be called as varname() and that won't work with your code.
To make it a real functor change your code a template class (functors are classes):
除了: 语法之外,我知道没有其他“直接”方式
。 如果您愿意更改代码,则可以将模板参数移动到类中,或者使用 (boost|tr1)::bind 来创建 (boost|tr1)::function 对象。
There's not another "direct" way I know other than the:
syntax. If you're open to changing the code, moving the template parameter to the class would work, or using a (boost|tr1)::bind to make a (boost|tr1)::function object.
您试图将模板参数传递给对象的实例,据我所知,这是不允许的。 您只能将模板参数传递给模板函数或模板对象。
a.测试<1>(); 和 a.operator()<1>(); 之所以有效,是因为它们充当模板函数。
使用 boost::bind (查看 boost 库)来修复它。
而且您甚至不必搞乱模板!
You are trying to pass a template parameter to an instance of an object, which as far as I know is not allowed. You can only pass templates parameters to template functions or template objects.
a.test<1>(); and a.operator()<1>(); work because they are serving as template functions.
Use boost::bind (check out boost libraries) to fix it though.
And you don't even have to mess with templates!
你被困住了。 你有没有考虑过类似的事情
You're stuck. Have you considered something like
不,没有办法解决这个问题。 正如您所说,您必须显式调用运算符(这违背了目的),或者模板参数必须能够由编译器推导。
Nope, there's no way around it. Like you said, you have to either call the operator explicitly (which defeats the purpose), or the template arguments must be able to be deduced by the compiler.