这是 lambda 表达式的有效(滥用)使用吗?

发布于 2024-11-08 07:49:07 字数 1157 浏览 5 评论 0原文

众所周知,如果没有

不过,你必须承认,所有这些都有点笨拙。特别是由于缺少调用循环的上下文,因此缺少函数版本,因为您需要将循环中所需的所有内容作为参数传递。
此外,对于每个嵌套循环,第二个循环都会变得更糟。
所以,我个人仍然认为 goto 版本是最干净的。


现在,考虑所有 C++0x 之类的东西,第三个选项给我带来了利用 lambda 表达式的想法:(

#include <iostream>

bool CheckCondition(){
  return true;
}

bool CheckOtherCondition(){
  return false;
}

int main(){
  [&]{while(CheckCondition()){
    for(;;){
      if(!CheckOtherCondition())
        return;
      // do stuff...
    }
    // do stuff...
  }}();
  std::cout << "yep, broke out of it\n";
}

Ideone 的示例。)

这允许第三个选项提供的简单 return 的语义美,同时不会受到上下文问题的影响,并且(几乎)与 goto 版本一样干净。它也比上述任何选项都更短(按字符计算)。


现在,在发现该语言的美丽(滥用)用法后,我学会了抑制喜悦,因为几乎总是存在某种缺点。有这方面的吗?或者有更好的方法来解决这个问题吗?

Like we all know, it's not that easy to break from a nested loop out of an outer loop without either:

Though, you gotta admit, all of those are kinda clumsy. Especially the function version lacks because of the missing context where the loops are called, as you'd need to pass everything you need in the loops as parameters.
Additionally, the second one gets worse for each nested loop.
So, I personally, still consider the goto version to be the cleanest.


Now, thinking all C++0x and stuff, the third option brought me this idea utilizing lambda expressions:

#include <iostream>

bool CheckCondition(){
  return true;
}

bool CheckOtherCondition(){
  return false;
}

int main(){
  [&]{while(CheckCondition()){
    for(;;){
      if(!CheckOtherCondition())
        return;
      // do stuff...
    }
    // do stuff...
  }}();
  std::cout << "yep, broke out of it\n";
}

(Example at Ideone.)

This allows for the semantic beauty of a simple return that the third option offers while not suffering from the context problems and being (nearly) as clean as the goto version. It's also even shorter (character-wise) than any of the above options.


Now, I've learned to keep my joy down after finding beautiful (ab)uses of the language, because there's almost always some kind of drawback. Are there any on this one? Or is there even a better approach to the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

鱼窥荷 2024-11-15 07:49:08

请不要在我管理的项目中这样做。在我看来,这是对 lambda 表达式的尴尬滥用。

goto 有用的地方使用 goto

Please don't do that in a project I'm managing. That's an awkward abuse of lambdas in my opinion.

Use a goto where a goto is useful.

萌化 2024-11-15 07:49:08

我认为完全正确。虽然我更喜欢为我的代码分配名称,使代码更加自我记录,即

int main(){

  auto DoThatOneThing = [&]{while(CheckCondition()){
    for(;;){
      if(!CheckOtherCondition())
        return;
      // do stuff...
    }
    // do stuff...
  }};

  DoThatOneThing();
  std::cout << "yep, broke out of it\n";
}

Perfectly valid in my opinion. Though I prefer to assign mine with names, making the code more self documenting, i.e.

int main(){

  auto DoThatOneThing = [&]{while(CheckCondition()){
    for(;;){
      if(!CheckOtherCondition())
        return;
      // do stuff...
    }
    // do stuff...
  }};

  DoThatOneThing();
  std::cout << "yep, broke out of it\n";
}
请爱~陌生人 2024-11-15 07:49:08

这是对

void frgleTheBrgls()
{
  while(CheckCondition()) {
    for(;;) {
      if(!CheckOtherCondition())
        return;
      // do stuff...
    }
    // do stuff...
  }
}

int main()
{
  frgleTheBrgls();
  std::cout << "yep, broke out of it\n";
}

This 的改进,这是众所周知的(函数,你知道,就像 BASIC 中的那样),更清晰(该算法有一个很好的名字来解释它的作用),并且与你的完全一样。

特别是缺少函数版本,因为缺少调用循环的上下文,因为您需要将循环中所需的所有内容作为参数传递。

我认为这是一个优势。您清楚地看到需要什么来消除 brgls。编程时,明确性通常是一件好事。

In which way is that an improvement over

void frgleTheBrgls()
{
  while(CheckCondition()) {
    for(;;) {
      if(!CheckOtherCondition())
        return;
      // do stuff...
    }
    // do stuff...
  }
}

int main()
{
  frgleTheBrgls();
  std::cout << "yep, broke out of it\n";
}

This is much well-known (functions, you know, as in BASIC), clearer (the algorithm's got a nice name explaining what it does), and does exactly the same as yours does.

Especially the function version lacks because of the missing context where the loops are called, as you'd need to pass everything you need in the loops as parameters.

I see that as an advantage. You see exactly what is needed to frgle the brgls. Explicity, when programming, often is a good thing.

泡沫很甜 2024-11-15 07:49:08

您提出的语法的一个缺点是:您不能有超过 2 个嵌套循环。 “goto”语法允许这样做:

int main()
{
    for (;;)
    {
        for (;;)
        {
            for (;;)
            {
                if (CheckCondition1()) goto BREAK_ON_COND1;
                if (CheckCondition2()) goto BREAK_ON_COND2;
                if (CheckCondition3()) break;
                // Do stuff when all conditions are false
            }
            // Do stuff when condition 3 becomes true
        }
    BREAK_ON_COND2:
        // Do stuff when condition 2 becomes true
    }
BREAK_ON_COND1: // When condition 1 becomes true
    std::cout << "yep, broke out of it\n";
}

One drawback with your proposed syntax: you cannot have more than 2 nested loops. The 'goto' syntax allows this:

int main()
{
    for (;;)
    {
        for (;;)
        {
            for (;;)
            {
                if (CheckCondition1()) goto BREAK_ON_COND1;
                if (CheckCondition2()) goto BREAK_ON_COND2;
                if (CheckCondition3()) break;
                // Do stuff when all conditions are false
            }
            // Do stuff when condition 3 becomes true
        }
    BREAK_ON_COND2:
        // Do stuff when condition 2 becomes true
    }
BREAK_ON_COND1: // When condition 1 becomes true
    std::cout << "yep, broke out of it\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文