简体   繁体   English

c++11 lambdas 的代码可读性

[英]Code readability with c++11 lambdas

I REALLY love lambdas and having the ability to use them in C++ is a pleasure.我真的很喜欢 lambda,并且能够在 C++ 中使用它们是一种乐趣。 But, as I'm used to Haskell, where lambdas fit really well into the syntax, I'm struggling with how to use them in C++ without writing unreadable cluttered long code lines.但是,由于我习惯了 Haskell,其中 lambda 非常适合语法,我正在努力解决如何在 C++ 中使用它们而不编写不可读的杂乱长代码行。

So, as an example, suppose I'd write this:所以,作为一个例子,假设我会这样写:

vector<double> foo(10,0.2);
for_each(foo.begin(), foo.end(), [](double x){ std::cout << x << " ";})

this is not so difficult to read, the lambda expression is pretty small.这并不难读,lambda 表达式很小。 But if I have a two or three line long function inside that for_each, this could become a problem for my code-reading-skills:但是,如果我在 for_each 中有两三行长的 function,这可能会成为我的代码阅读技能的问题:

vector<double> foo(10,0.2);
randomNumberGenerator bar;
for_each(foo.begin(), foo.end(), [](double x){ std::cout << "hello!"; x+=bar()/(1+bar()); std::cout << x << " ";})
//sorry, I couldn't think of a less stupid example... 

This line is starting to get annoyingly long and difficult to read for my taste...根据我的口味,这条线开始变得烦人且难以阅读......

What is your preferred code conventions for this case?对于这种情况,您首选的代码约定是什么? Should I write:我应该写:

for_each(foo.begin(), foo.end(), 
          [] (double x) {
                std::cout << "hello!"
                x += bar()/(1+bar());
                std::cout << x << " ";
          });

or something like it?或类似的东西? I still think this syntax feels a bit unnatural and difficult to read... :(我仍然认为这种语法感觉有点不自然且难以阅读...... :(

I usually go for我通常为 go

for_each(foo.begin(), foo.end(), [](double x) {
    std::cout << "hello!"
    x += bar()/(1+bar());
    std::cout << x << " ";
});

I've written some several hundred line lambdas.我已经写了几百行 lambda。

If you prefer, you can name your lambda separately with auto :如果您愿意,可以使用auto分别命名您的 lambda :

auto const baz = [](double x)
{
    std::cout << "hello!"
    x += bar()/(1+bar());
    std::cout << x << " ";
};
std::for_each(foo.begin(), foo.end(), baz);

Hmm...唔...

for_each(foo.begin(), foo.end(), 
    [] (double x)
    {
        std::cout << "hello!"
        x += bar()/(1+bar());
        std::cout << x << " ";
    });

for (auto x : foo)
{
    std::cout << "hello!";
    x += bar()/(1+bar());
    std::cout << x << " ";
}

I like to look at lambdas as just another function declaration, and thus, follow the same conventions that I use for other functions, within reason:我喜欢将 lambdas 视为另一个 function 声明,因此,在合理范围内遵循我用于其他函数的相同约定:

// when lambdas are present, I break the enveloping method params
for_each(
  foo.begin(), 
  foo.end(),           
  [] (double x)
  // I also like to split the brackets, just like with any function
  {
     std::cout << "hello!" 
     x += bar()/(1+bar());                
    std::cout << x << " ";          
  }); // the closing parenthesis is left with the closing bracket

I'd say if the code for the lambda is more than one or perhaps two statements, it should be a separate named function.我想说如果 lambda 的代码不止一个或两个语句,它应该是一个单独的名为 function 的语句。

Post mine发布我的

std::vector<int> a;
std::find_if(a.begin()
           , a.end()
           , [&](int i)
             {
                 return i == 0;
             });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM