I do not understand why this code
#include <iostream>
class A {
public:
void foo(){
char g = 'm';
switch(g){
case 'g':
auto f = [](){std::printf("hello world\n");};
f();
break;
}
};
};
int main(int iargc, char *iargv[]){
A a;
a.foo();
}
compiles (and works) perfectly, while when uncommenting the default instructions
#include <iostream>
class A {
public:
void foo(){
char g = 'm';
switch(g){
case 'g':
auto f = [](){std::printf("hello world\n");};
f();
break;
default:
std::printf("go to hell\n");
break;
}
};
};
int main(int iargc, char *iargv[]){
A a;
a.foo();
}
gives the following error message
test.cpp:15:13: error: jump to case label [-fpermissive]
default:
^
test.cpp:12:22: error: crosses initialization of ‘A::foo()::__lambda0 f’
auto f = [](){std::printf("hello world\n");};
I can use the default instruction if I comment on the lambda function.
I am using gcc 4.8.5.
source
share