Hi, All
Recently I try to use std::function and Lambda expression to simply my code.
However there are some weird problems.
#include <cstdio>
#include <vector>
#include <functional>
int main(int argc, char *argv[])
{
std::function<bool (int)> arr[]={
[&](int n)->bool {
std::wstring y;
printf("function 0, %d\n", n);
return false;
},
[&](int n)->bool {
std::wstring y;
printf("function 1, %d\n", n);
return false;
}
};
arr[1](100);
return 0;
}
Above code can be compiled by VS2010, but crush during run-time.
I found the internal pointer _Impl of function template holds invalid address.
If I define an array in lambda functions, the program crashes at runtime with C1001.
The following code, does not crash, only difference, used
int x[100] instead of
std::wstring y:
#include <cstdio>
#include <vector>
#include <functional>
int main(int argc, char *argv[])
{
std::function<bool (int)> arr[]={
[&](int n)->bool {
int x[100]={0};
printf("function 0, %d\n", n);
return false;
},
[&](int n)->bool {
int x[100]={0};
printf("function 1, %d\n", n);
return false;
}
};
arr[1](100);
return 0;
}
Any advice will be appreciated.