这是 lambda 表达式的有效(滥用)使用吗?
众所周知,如果没有
不过,你必须承认,所有这些都有点笨拙。特别是由于缺少调用循环的上下文,因此缺少函数版本,因为您需要将循环中所需的所有内容作为参数传递。
此外,对于每个嵌套循环,第二个循环都会变得更糟。
所以,我个人仍然认为 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";
}
这允许第三个选项提供的简单 return
的语义美,同时不会受到上下文问题的影响,并且(几乎)与 goto
版本一样干净。它也比上述任何选项都更短(按字符计算)。
现在,在发现该语言的美丽(滥用)用法后,我学会了抑制喜悦,因为几乎总是存在某种缺点。有这方面的吗?或者有更好的方法来解决这个问题吗?
Like we all know, it's not that easy to break
from a nested loop out of an outer loop without either:
- a
goto
(Example code.) - another condition check in the outer loop (Example code.)
- putting both loops in an extra function and returning instead of
break
ing (Example code.)
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";
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请不要在我管理的项目中这样做。在我看来,这是对 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 agoto
is useful.我认为完全正确。虽然我更喜欢为我的代码分配名称,使代码更加自我记录,即
Perfectly valid in my opinion. Though I prefer to assign mine with names, making the code more self documenting, i.e.
这是对
This 的改进,这是众所周知的(函数,你知道,就像 BASIC 中的那样),更清晰(该算法有一个很好的名字来解释它的作用),并且与你的完全一样。
我认为这是一个优势。您清楚地看到需要什么来消除 brgls。编程时,明确性通常是一件好事。
In which way is that an improvement over
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.
I see that as an advantage. You see exactly what is needed to frgle the brgls. Explicity, when programming, often is a good thing.
您提出的语法的一个缺点是:您不能有超过 2 个嵌套循环。 “goto”语法允许这样做:
One drawback with your proposed syntax: you cannot have more than 2 nested loops. The 'goto' syntax allows this: