Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
std::在堆栈上的数组中使用时函数崩溃 在Visual C++ 2010 SP1中,该代码崩溃: #include "stdafx.h" #include <functional> #include <iostream> //#include <vector> int a = 0; int _tmain(int argc, _TCHAR* argv[]) { // this way it works: //std::vector<std::function<void ()>> s; //s.push_back([]() { a = 1; }); //s.push_back([]() { a = 2; int b = a; }); std::function<void ()> s[] = { []() { a = 1; }, []() { a = 2; // Problem occurs only if the following line is included. When commented out no problem occurs. int b = a; } }; int counter = 0; for (auto it = std::begin(s); it != std::end(s); ++it) { ++counter; (*it)(); std::wcout << counter << L":" << a << std::endl; } return 0; } #包括“stdafx.h” #包括 #包括 //#包括 int a=0; int _tmain(int argc,_TCHAR*argv[]{ //通过这种方式工作: //std::向量s; //s、 向后推([](){a=1;}); //s、 向后推([])({a=2;int b=a;}); std::函数s[]={ [](){a=1;}, []() { a=2; //只有在包含以下行时才会出现问题。注释掉后不会出现问题。 int b=a; } }; int计数器=0; 用于(自动it=std::begin(s);it!=std::end(s);+it){ ++计数器; (*it)(); std::wcout_C++_Visual Studio 2010_Visual C++_C++11 - Fatal编程技术网

std::在堆栈上的数组中使用时函数崩溃 在Visual C++ 2010 SP1中,该代码崩溃: #include "stdafx.h" #include <functional> #include <iostream> //#include <vector> int a = 0; int _tmain(int argc, _TCHAR* argv[]) { // this way it works: //std::vector<std::function<void ()>> s; //s.push_back([]() { a = 1; }); //s.push_back([]() { a = 2; int b = a; }); std::function<void ()> s[] = { []() { a = 1; }, []() { a = 2; // Problem occurs only if the following line is included. When commented out no problem occurs. int b = a; } }; int counter = 0; for (auto it = std::begin(s); it != std::end(s); ++it) { ++counter; (*it)(); std::wcout << counter << L":" << a << std::endl; } return 0; } #包括“stdafx.h” #包括 #包括 //#包括 int a=0; int _tmain(int argc,_TCHAR*argv[]{ //通过这种方式工作: //std::向量s; //s、 向后推([](){a=1;}); //s、 向后推([])({a=2;int b=a;}); std::函数s[]={ [](){a=1;}, []() { a=2; //只有在包含以下行时才会出现问题。注释掉后不会出现问题。 int b=a; } }; int计数器=0; 用于(自动it=std::begin(s);it!=std::end(s);+it){ ++计数器; (*it)(); std::wcout

std::在堆栈上的数组中使用时函数崩溃 在Visual C++ 2010 SP1中,该代码崩溃: #include "stdafx.h" #include <functional> #include <iostream> //#include <vector> int a = 0; int _tmain(int argc, _TCHAR* argv[]) { // this way it works: //std::vector<std::function<void ()>> s; //s.push_back([]() { a = 1; }); //s.push_back([]() { a = 2; int b = a; }); std::function<void ()> s[] = { []() { a = 1; }, []() { a = 2; // Problem occurs only if the following line is included. When commented out no problem occurs. int b = a; } }; int counter = 0; for (auto it = std::begin(s); it != std::end(s); ++it) { ++counter; (*it)(); std::wcout << counter << L":" << a << std::endl; } return 0; } #包括“stdafx.h” #包括 #包括 //#包括 int a=0; int _tmain(int argc,_TCHAR*argv[]{ //通过这种方式工作: //std::向量s; //s、 向后推([](){a=1;}); //s、 向后推([])({a=2;int b=a;}); std::函数s[]={ [](){a=1;}, []() { a=2; //只有在包含以下行时才会出现问题。注释掉后不会出现问题。 int b=a; } }; int计数器=0; 用于(自动it=std::begin(s);it!=std::end(s);+it){ ++计数器; (*it)(); std::wcout,c++,visual-studio-2010,visual-c++,c++11,C++,Visual Studio 2010,Visual C++,C++11,捕获位在哪里?如果要修改a,在第一个lambda开始时不需要[&a]吗 [&a]() { a = 1; }, 这是一个编译器错误。您的代码没有问题 程序使用Visual C++ 11 beta编译和运行,因此bug似乎已经被编译器的修复所释放。我不知道答案,但是FWW,一旦你删除了MSVC细节,就在GCC 4.6上工作。谢谢你用GCC 4.6进行测试。这给MS编译器中的bug提供了一些证据。事实上,我对lambda函数的OUTIND是混乱的,我把问题最小化到了那个代码中:α包含了int

捕获位在哪里?如果要修改a,在第一个lambda开始时不需要[&a]吗

[&a]() { a = 1; },

这是一个编译器错误。您的代码没有问题


程序使用Visual C++ 11 beta编译和运行,因此bug似乎已经被编译器的修复所释放。

我不知道答案,但是FWW,一旦你删除了MSVC细节,就在GCC 4.6上工作。谢谢你用GCC 4.6进行测试。这给MS编译器中的bug提供了一些证据。事实上,我对lambda函数的OUTIND是混乱的,我把问题最小化到了那个代码中:α包含了int main(int ARC,char *ARGV[]){STD::[s]= {[] ](ST{:St:::CUTE),这是一个bug。程序使用Visual C++ 11 beta编译并运行无错误。(这可能是对您的请求的响应。)“JaveMcNeLIS我会接受你的评论。”代码< < <代码>不应该被捕获:它是一个全局变量。如果A是不可访问的,那么它应该是编译时错误而不是运行时错误。希望它也能修复C++ 2010。我们不能使用C++ 11,因为编译的程序不能在WindowsXP上运行。@ FrAST:我不会。屏住呼吸。这不太可能保证VC10的补丁,特别是因为有一个简单的解决方法(例如,使用
std::vector
,如您在第二个示例中所做的,或者单独初始化数组元素,而不是使用聚合初始化)。
[&a]() { a = 1; },