C++0x 闭包/lambda 示例
我试图利用 C++0x 闭包使自定义词法分析器和解析器之间的控制流更加简单。如果没有闭包,我有以下安排:
//--------
// lexer.h
class Lexer {
public:
struct Token { int type; QString lexeme; }
struct Callback {
virtual int processToken(const Token &token) = 0;
};
Lexer();
int tokenize(const QList<Token> &patterns, QTextStream &stream,
Callback *callback);
};
//-------------
// foo_parser.h
class FooParser: public Lexer::Callback {
virtual int processToken(const Lexer::Token &token);
int process(QTextStream *fooStream);
// etc..
}
//--------------
// foo_parser.cc
int FooParser::processToken(const Lexer::Token &token) {
canonicalize(token);
processLine();
return 0;
}
int FooParser::process(QTextStream *fooStream) {
Lexer lexer;
// *** Jumps to FooParser::processToken() above! ***
return lexer.tokenize(patterns_, fooStream, this);
}
上面代码的主要问题是我不喜欢控制流中从 lexer.tokenize() 调用到 FooParser::processToken() 函数的“跳转”。
我希望闭包能够允许这样的事情:
int FooParser::process(QTextStream *fooStream) {
Lexer lexer;
return lexer.tokenize(patterns_, fooStream, [&](const Lexer::Token &token) {
canonicalize(token);
processLine();
return 0;
});
// ...
}
至少对我来说,通过 lexer.tokenize() 调用哪些 FooParser 方法更加清楚。
不幸的是,我见过的 C++0x 闭包的唯一示例是这样的:
int total = 0;
std::for_each(vec.begin(), vec.end(), [&total](int x){total += x;});
printf("total = %d\n", total);
虽然我可以让这个示例代码工作,但我一直无法弄清楚如何编写函数 >像 std::for_each() 一样,它接受 Functor/closure 作为参数并调用它。
也就是说,我不确定如何编写一个类 Foo 以便我可以执行此操作:
// Does this need to be templated for the Functor?
struct Foo {
void doStuff( ... what goes here?????? ) {
myArg();
}
};
int someNumber = 1234;
Foo foo;
foo.doStuff([&]() { printf("someNumber = %d\n", someNumber); }
对于此示例,预期输出将是 someNumber = 1234
对于参考,我的编译器是 gcc 版本 4.5.1 。
非常感谢。
I am attempting to leverage C++0x closures to make the control flow between a custom lexer and parser more straightforward. Without closures, I have the following arrangement:
//--------
// lexer.h
class Lexer {
public:
struct Token { int type; QString lexeme; }
struct Callback {
virtual int processToken(const Token &token) = 0;
};
Lexer();
int tokenize(const QList<Token> &patterns, QTextStream &stream,
Callback *callback);
};
//-------------
// foo_parser.h
class FooParser: public Lexer::Callback {
virtual int processToken(const Lexer::Token &token);
int process(QTextStream *fooStream);
// etc..
}
//--------------
// foo_parser.cc
int FooParser::processToken(const Lexer::Token &token) {
canonicalize(token);
processLine();
return 0;
}
int FooParser::process(QTextStream *fooStream) {
Lexer lexer;
// *** Jumps to FooParser::processToken() above! ***
return lexer.tokenize(patterns_, fooStream, this);
}
The main issue I have with the above code is that I don't like the "jump" in control flow from the lexer.tokenize() call to the FooParser::processToken() function.
I am hoping that closures will allow something like this:
int FooParser::process(QTextStream *fooStream) {
Lexer lexer;
return lexer.tokenize(patterns_, fooStream, [&](const Lexer::Token &token) {
canonicalize(token);
processLine();
return 0;
});
// ...
}
At least to me, it's a lot more clear what FooParser methods will be invoked via lexer.tokenize() .
Unfortunately the only examples I have seen with C++0x closures go something like this:
int total = 0;
std::for_each(vec.begin(), vec.end(), [&total](int x){total += x;});
printf("total = %d\n", total);
And while I can get this example code to work, I have been unable to figure out how to write a function like std::for_each() that takes a Functor/closure as an argument and invokes it.
That is to say, I'm not sure how to write a class Foo such that I can do this:
// Does this need to be templated for the Functor?
struct Foo {
void doStuff( ... what goes here?????? ) {
myArg();
}
};
int someNumber = 1234;
Foo foo;
foo.doStuff([&]() { printf("someNumber = %d\n", someNumber); }
For this example, the expected output would be someNumber = 1234
For reference, my compiler is gcc version 4.5.1 .
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
doStuff
可以采用std::function
:使用模板是另一种选择:
lambda 表达式的实际类型是唯一且未指定的。
doStuff
can take astd::function
:Using a template is another option:
The actual type of the lambda expression is unique and unspecified.