简体   繁体   English

lambda表达式(MSVC ++ vs g ++)

[英]lambda expression (MSVC++ vs g++)

I have the following code 我有以下代码

#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>


int main()
{
  typedef std::vector<int> Vector; 
  int sum=0;
  Vector v;
  for(int i=1;i<=10;++i)
     v.push_back(i);

  std::tr1::function<double()>  l=[&]()->double{

    std::for_each(v.begin(),v.end(),[&](int n){sum += n; //Error Here in MSVC++});
    return sum;
     };

  std::cout<<l();
  std::cin.get();
}

The above code produces an error on MSVC++ 10 whereas it compiles fine with g++ 4.5 . 上面的代码在MSVC++ 10上产生错误,而它在g++ 4.5编译得很好。 The error produced is 1 IntelliSense: invalid reference to an outer-scope local variable in a lambda body c:\\users\\super user\\documents\\visual studio 2010\\projects\\lambda\\lambda.cpp 19 46 lambda 产生的错误是1 IntelliSense: invalid reference to an outer-scope local variable in a lambda body c:\\users\\super user\\documents\\visual studio 2010\\projects\\lambda\\lambda.cpp 19 46 lambda

So, is there any other way to access the outer-scope variable sum without explicitly creating a new variable inside the local lambda expression(inside std::for_each )? 那么,有没有其他方法来访问外部范围变量sum而不在本地lambda表达式中显式创建一个新变量(在std::for_each )?

On g++ 4.5 the code compiles fine. g++ 4.5 ,代码编译得很好。 Does the standard(n3000 draft) say anything about it?(I don't have a copy of C++-0x(1x ?) standard at present) 标准(n3000草案)是否说了什么?(我目前没有C ++ - 0x(1x?)标准的副本)

Have you actually tried compiling the code in the question? 你真的尝试过编译问题中的代码吗? Visual C++ 2010 accepts the code, as is (with the comment removed, obviously), and successfully compiles the code without error. Visual C ++ 2010接受代码(显然删除了注释),并成功编译代码而没有错误。

The "error" you are seeing is not a compilation error, but an IntelliSense error. 您看到的“错误”不是编译错误,而是IntelliSense错误。 The IntelliSense error checking results in a lot of false positives (I've reported several bugs on Microsoft Connect over the past few months); IntelliSense错误检查会导致很多误报(我在过去几个月中报告过Microsoft Connect上的一些错误); in this case, IntelliSense is incorrectly saying this is an error when it is not. 在这种情况下,IntelliSense错误地说这是一个错误,当它不是。

You have two options: you can ignore the IntelliSense false positives or you can disable the IntelliSense error checking (right-click the Error List window and uncheck "Show IntelliSense Errors"). 您有两个选择:可以忽略IntelliSense误报,也可以禁用IntelliSense错误检查(右键单击错误列表窗口并取消选中“显示IntelliSense错误”)。

Either way, these IntelliSense errors in no way prevent compilation from succeeding. 无论哪种方式,这些IntelliSense错误都不会阻止编译成功。

Regardless of whether VC is wrong or right, it's bad style that you have sum declared outside your (outer) lambda. 无论VC是错还是正确,你在(外)lambda之外声明的总和是不好的风格。 Since you return the value of sum, there's no need to be changing the value of an outer variable inside the loop. 由于返回sum的值,因此无需在循环内更改外部变量的值。 Instead, you should have: 相反,你应该:

int sum = 0;
std::for_each(v.begin(),v.end(),[&](int n){sum += n;});
return sum;

It could be that the nested lambdas are confusing VC, too. 可能是嵌套的lambdas也让VC感到困惑。 I'd say it's overkill to have nested lambdas, and makes for less readable code. 我会说嵌套lambda是不合适的,并使代码不太可读。

I think you may have to explicitly declare the closure over sum , like so: 我认为你可能必须明确地声明关闭sum ,如下:

std::for_each(v.begin(),v.end(),[&sum](int n){sum += n;});

In general, you're supposed to be allowed to implicitly capture variables in the local scope, but only as long as the lambda is guaranteed to run in the same scope. 通常,您应该被允许隐式捕获本地范围中的变量,但只要保证lambda在同一范围内运行。 Possibly because you're assigning your lambda to a function var and executing it later (instead of just running it directly), MSVC isn't smart enough to understand that that condition holds - after all, you could potentially pass l and execute it in some other scope - so it requires the explicit capture declaration. 可能是因为你将lambda分配给一个函数var并在以后执行它(而不是直接运行它),MSVC不够聪明,无法理解该条件成立 - 毕竟,你可能会传递l并执行它其他一些范围 - 所以它需要明确的捕获声明。

I think the sole problem you have is with that ant size red wave.... SINCE microsoft had released the compiler earlier and soon the standards body did change the rule for name look up ...so intellisense isnt upto date........ 我认为你唯一的问题就是那个蚂蚁大小的红色波浪....... SINCE微软早些时候发布了编译器,很快标准组织确实改变了名称查找的规则...所以intellisense不是最新的.... ....

SO TRY HITTING WITH THIS IDEA.....BABY... 所以,请尝试这个想法..... BABY ......

#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>


int main()
{
  typedef std::vector<int> Vector; 
  int sum=0;
  Vector v;
  for(int i=1;i<=10;++i)
     v.push_back(i);

  std::tr1::function<double()>  l=[&]()->double{
      int *y; y=&sum;
      std::for_each(v.begin(),v.end(),[&](int n){*y += n; });
    return sum;
     };

  std::cout<<l();
  std::cin.get();
}

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

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