5

I'm trying to create an array of functors at compile time, like so: (complete file):

#include <functional>
using namespace std;

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

int main()
{
}

That works fine. But as soon as you try to create a local inside the functor block, like this:

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

You get Error 1 error C1506: unrecoverable block scoping error

How can I declare locals inside these blocks? It doesn't seem to work.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • 1
    I killed the parameters because of them being unused and GCC gives no warnings or anything. Even with, the only error is for unused parameters. – chris Jun 30 '12 at 21:57
  • 1
    Is it valid to have that comma at the end of lambda expression? – coelhudo Jun 30 '12 at 21:59
  • 2
    @coelhudo, It's an initializer list; [it's valid](http://stackoverflow.com/questions/7043372/int-a-1-2-weird-comma-allowed-any-particular-reason). It makes it easier to add items later. – chris Jun 30 '12 at 21:59
  • 2
    It would be pretty helpful to specify exactly what compiler version (including service packs) you're using (by the error code & the C++11 features I guess you're using some VS2010). – Eugen Constantin Dinca Jun 30 '12 at 22:03
  • 9
    This is 99% likely to be a bug in VC++ 2010. Have you tried with VC++ 2012? – ildjarn Jun 30 '12 at 23:08
  • 3
    It works on Microsoft Visual Studio Ultimate 2012 RC Version 11.0.50522.1 RCREL. – Alessandro Jacopson Jul 02 '12 at 07:36
  • It works perfectly on gcc 4.7.1 on Debian, it should be a bug with your compiler – PLuS Aug 08 '12 at 23:25
  • It also works with Clang 3.1. Chances are high that it's a compiler problem. – Nikos C. Aug 29 '12 at 22:45

2 Answers2

0

I can reproduce this on MSVC 2010, SP1. VS10 is known for some problems with lambdas and scoping. I've tried around a lot but found nothing beautiful. Ugly, ugly workaround that will have some initialization overhead but else work 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';
}
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
-1

I compiled the following code on ubuntu 12.04 with the following line:
g++-4.7 -std=c++0x main.cpp
And it worked fine. What platform and what compiler are you using?

#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;
}
Tal Zion
  • 1,331
  • 2
  • 14
  • 23