Reputation: 36639
I am trying to compile my code in VS 2010 that compiles and works in gcc, but I get a weird error on a lambda expression:
std::function<double (size_t level, size_t q)> doLevel = [&](size_t level, size_t q)
{ //line 52
double thisLevelP = density(q, data[level]);
if(level==T-1) return thisLevelP;
if(level==t-1) return thisLevelP*A(q,state)*doLevel(level+1,state);
double continuation = 0.0;
for(size_t i=0; i<Q; ++i)
{
continuation += A(q, i)*doLevel(level+1, i);
}
return thisLevelP*continuation;
};
The error is:
Error 1 error C2086: 'size_t &_T' : redefinition c:\users\ga1009\documents\phd\cpp\pmi\cpp\test\baumiterationtest.cpp 52
Error 2 error C2661: '`anonymous-namespace'::`anonymous-namespace'::<lambda1>::<lambda1>' : no overloaded function takes 8 arguments c:\users\ga1009\documents\phd\cpp\pmi\cpp\test\baumiterationtest.cpp 52
What is wrong?
Upvotes: 0
Views: 328
Reputation: 5552
As seen in the comments above by chris, lambdas with multiple return statements need an explicit return type which can be declared like this ->double
.
More importantly though the offending line is the variable named t
which is captured by the lambda. I originally though this was a name clash but as STL correctly points out it happens with any two names which are the same except one is capital. This is a compiler bug which is known and has been fixed in the newest versions.
double density(std::size_t, int)
{
return 0;
}
double A(std::size_t, int)
{
return 0;
}
int main(int argc, char *argv[])
{
int data[] = {1,2,3};
std::size_t T = 1;
std::size_t t = 2; //<<<<< change this name and it compiles
std::size_t state = 3;
std::size_t Q = 4;
std::function<double (int,int)> doLevel = [&](int level, int q)->double
{
double thisLevelP = density(q, data[level]);
if(level==T-1)
return thisLevelP;
if(level==t-1)
return thisLevelP*A(q,state)*doLevel(level+1,state);
double continuation = 0.0;
for(size_t i=0; i<Q; ++i)
{
continuation += A(q, i)*doLevel(level+1, i);
}
return thisLevelP*continuation;
};
}
Upvotes: 4