C++0x 闭包/lambda 示例

发布于 2024-09-29 04:43:02 字数 2013 浏览 3 评论 0原文

我试图利用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2024-10-06 04:43:02

doStuff 可以采用 std::function

void doStuff(std::function<void()> f)
{
    f();
}

使用模板是另一种选择:

template <typename FunctionT>
void doStuff(FunctionT f)
{
    f();
}

lambda 表达式的实际类型是唯一且未指定的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文