C ++ 0x闭包/ lambdas示例 [英] C++0x closures / lambdas example
问题描述
我试图利用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
类FooParser:public Lexer :: Callback {
virtual int processToken(const Lexer :: Token& token);
int进程(QTextStream * fooStream);
//等..
}
// --------------
// foo_parser.cc
int FooParser: :processToken(const Lexer :: Token& token){
canonicalize(token);
processLine();
return 0;
}
int FooParser :: process(QTextStream * fooStream){
Lexer lexer;
// ***跳转到FooParser :: processToken()上面! ***
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;
});
// ...
}
至少对我来说,更多清楚什么FooParser方法将通过lexer.tokenize()调用。不幸的是,我用C ++ 0x闭包看到的唯一例子是这样的:
int total = 0;
std :: for_each(vec.begin(),vec.end(),[& total](int x){total + = x;});
printf(total =%d\\\
,total);
虽然我可以获得这个示例代码的工作,无法弄清楚如何编写一个函数如 std :: for_each(),它将Functor / closure作为参数并调用它。
也就是说,我不知道如何写一个类 Foo ,以便我可以这样做:
//这需要为Functor模板化吗?
struct Foo {
void doStuff(...这里做什么){
myArg();
}
};
int someNumber = 1234;
Foo foo;
foo.doStuff([&](){printf(someNumber =%d\\\
,someNumber);}
对于此示例,预期输出将为 someNumber = 1234
作为参考,我的编译器是gcc版本4.5.1。
非常感谢。
doStuff
可以使用 std :: function
:
void doStuff(std :: function< void()> f)
{
f();
}
使用模板是另一个选项:
template< typename FunctionT>
void doStuff(FunctionT f)
{
f();
}
lambda表达式的实际类型是唯一的,未指定。
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.
doStuff
can take a std::function
:
void doStuff(std::function<void()> f)
{
f();
}
Using a template is another option:
template <typename FunctionT>
void doStuff(FunctionT f)
{
f();
}
The actual type of the lambda expression is unique and unspecified.
这篇关于C ++ 0x闭包/ lambdas示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!