bob*_*obo 5 c++ arrays lambda functor c++11
我正在尝试在编译时创建一个仿函数数组,如下所示:(完整文件):
#include <functional>
using namespace std;
function< float( float tElevation, float pAzimuth )> colorFunctions[] = {
[]( float tElevation, float pAzimuth ) -> float {
return 2.0f ;
},
} ;
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
这很好.但是只要你尝试在仿函数块中创建一个局部,就像这样:
function< float( float tElevation, float pAzimuth )> colorFunctions[] = {
[]( float tElevation, float pAzimuth ) -> float {
float v = 2.0f ;
return v ;
},
} ;
Run Code Online (Sandbox Code Playgroud)
您收到错误1错误C1506:不可恢复的块范围错误
如何在这些块中声明本地?它似乎不起作用.
Tal*_*ion -1
我在 ubuntu 12.04 上使用以下行编译了以下代码:
g++-4.7 -std=c++0x main.cpp
并且运行良好。您使用什么平台和什么编译器?
#include <iostream>
#include <functional>
using namespace std;
function<float (float,float)> funcs[] = {
[] (float a, float b) -> float {
float c = 2.0f;
return c;
}
};
int main() {
std::cout << funcs[0](1,2) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)