Error of unused block error, array of C ++ functions

I am trying to create an array of functors at compile time, for example: (full file):

#include <functional>
using namespace std;

function< float( float tElevation, float pAzimuth )> colorFunctions[] = {
  []( float tElevation, float pAzimuth ) -> float {
    return 2.0f ;
  },
} ;

int main()
{
}

It works great. But as soon as you try to create a local one inside the functor block, for example:

function< float( float tElevation, float pAzimuth )> colorFunctions[] = {
  []( float tElevation, float pAzimuth ) -> float {
    float v = 2.0f ;
    return v ;
  },
} ;

You get the error Error 1 C1506: error repairing an unrecoverable block

How can I declare local residents inside these blocks? This does not seem to work.

+5
source share
2 answers

I can reproduce this on MSVC 2010, SP1. VS10 is known for some problems with lambdas and scope. I tried a lot, but found nothing. An ugly, ugly workaround that will have some initialization overhead, but also works as intended:

#include <functional>
#include <boost/assign/list_of.hpp>
#include <vector>
using namespace std;

typedef function< float( float tElevation, float pAzimuth )> f3Func;
vector<f3Func const> const colorFunctions = boost::assign::list_of(
  f3Func([]( float /*tElevation*/, float /*pAzimuth*/ ) -> float {
    float v = 2.0f ;
    return v ;
  }))
  ([](float a, float b) -> float {
    float someFloat = 3.14f;
    return a*b*someFloat;
  })
;

#include <iostream>

int main()
{
  cout << colorFunctions[1](0.3f,0.4f) << '\n';
}
0

ubuntu 12.04 :
g++ - 4.7 -std = ++ 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;
}
-1

All Articles