模板 C++0x lambda 函数...还是函子/谓词?
我最近升级了我的 g++
,这样我就可以享受 lambda 函数了。 一切都很棒,我非常感谢那些在 C++ 和 gcc 中使这一切成为可能的人。只有一件事我似乎无法解决 - 如何对 lambda 参数进行模板化?下面是 lambda 用法的基本示例来演示该问题。
示例#1,一切都很美味:
#include <cstdio>
struct bar {
bar () {}
void say () {
printf ("bar::say()\n");
}
void say () const {
printf ("bar::say() const\n");
}
};
template <typename T>
void do_work (const T & pred) {
bar b;
pred (b);
}
int main () {
do_work ([] (bar & b) { b.say (); });
}
现在,假设 do_work 现在使用不同的参数类型调用谓词两次。下面是示例#2:
#include <cstdio>
struct foo {
foo () {}
void say () {
printf ("foo::say()\n");
}
void say () const {
printf ("foo::say() const\n");
}
};
struct bar {
bar () {}
void say () {
printf ("bar::say()\n");
}
void say () const {
printf ("bar::say() const\n");
}
};
template <typename T>
void do_work (const T & pred) {
const foo f;
bar b;
pred (f);
pred (b);
}
int main () {
do_work ([] (auto & b) { b.say (); });
}
注意 auto
关键字。我也尝试就地对其进行模板化。不要尝试用 gcc 编译它,这就是我得到的:
./test.cpp:31:5: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
但你明白了。理论上,我可以用新的函数声明样式来解决它,但这不是重点。 是我真正想做的,但使用简化的语法(为了简单起见,删除了 foo 、 bar 和 do_work ):
struct pred_t {
pred_t () = default;
template <typename T>
void operator () (T && obj) const {
obj.say ();
}
};
int main () {
do_work (pred_t ());
}
这 一种方法,或者至少是计划,添加对不完全专用的 lambda 函数的支持,以便它们的行为有点像带有 template
?我什至不知道如何命名它,也许是 lambda 谓词?请让我知道您的想法!谢谢!
I have recently upgraded my g++
so I can enjoy lambda functions.
Everything is great and I am very thankful to those who made it possible in C++ and gcc in particular. There is only one thing that I can't seem to solve - how to have lambda's arguments to be templated? Below are basic examples of lambda usage to demonstrate the problem.
Example #1, everything is yummy:
#include <cstdio>
struct bar {
bar () {}
void say () {
printf ("bar::say()\n");
}
void say () const {
printf ("bar::say() const\n");
}
};
template <typename T>
void do_work (const T & pred) {
bar b;
pred (b);
}
int main () {
do_work ([] (bar & b) { b.say (); });
}
Now, assume that do_work
now invokes predicate two times with different argument types. So here goes example #2:
#include <cstdio>
struct foo {
foo () {}
void say () {
printf ("foo::say()\n");
}
void say () const {
printf ("foo::say() const\n");
}
};
struct bar {
bar () {}
void say () {
printf ("bar::say()\n");
}
void say () const {
printf ("bar::say() const\n");
}
};
template <typename T>
void do_work (const T & pred) {
const foo f;
bar b;
pred (f);
pred (b);
}
int main () {
do_work ([] (auto & b) { b.say (); });
}
Note auto
keyword. I also tried templating it in-place. Don't try to compile that with gcc, here is what I get:
./test.cpp:31:5: internal compiler error: Segmentation fault
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
But you get the idea. I can solve it with new function declaration style, in theory, but that's not the point. Here is what I really am trying to do, but with simplified syntax (foo
, bar
and do_work
are stripped for simplicity sake):
struct pred_t {
pred_t () = default;
template <typename T>
void operator () (T && obj) const {
obj.say ();
}
};
int main () {
do_work (pred_t ());
}
Is there a way, or at least plans, to add support for not fully specialized lambda functions, so that they will act sort of like a predicate with template <typename T> operator () (T &&)
? I don't even know how to name it, lambda predicate maybe? Please let me know your thoughts! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些被讨论为“多态 lambda”,并被拒绝,因为它对概念提案提出了问题。
去年2009年法兰克福会议,概念提案被否决工作论文,但 C++0x 不再考虑多态 lambda。请参阅 C++0x 和缺乏多态 Lambda。
These were discussed as "polymorphic lambdas", and were declined because it made problems with the concepts proposal.
Last year2009 at the Frankfurt meeting, the concepts proposal was voted out of the working paper, but polymorphic lambdas weren't considered anymore for C++0x.See C++0x and the Lack of Polymorphic Lambdas.
我认为 function/lambda 参数中不允许使用
auto
关键字。I think the
auto
keyword is not allowed in function/lambda parameter.