我正在使用VS2013,并且发现在使用包含lambdas的类的多个实例时,我认为是奇怪的行为,并且这些lambdas包含静态变量。静态变量似乎是共享的。
示例代码,非常精简但仍然捕获了本质:
class HasLambda
{
public:
typedef const char* ( *ToCharPtr ) ( const int& );
void Init( ToCharPtr pfnToCharPtr ) {
m_pfnCharPtrConverter = pfnToCharPtr;
}
const char* IntToString( int i ) {
return m_pfnCharPtrConverter( i );
}
static HasLambda* Make() {
HasLambda* pHasLambda = new HasLambda;
pHasLambda->Init( [] ( const int &i ) -> const char* { static char buf[ 33 ]; sprintf( buf, "%d", i ); return buf; } );
return pHasLambda;
}
protected:
ToCharPtr m_pfnCharPtrConverter;
};
int _tmain(int argc, _TCHAR* argv[])
{
HasLambda* a;
a = HasLambda::Make();
HasLambda* b;
b = HasLambda::Make();
const char* aValue = a->IntToString( 7 );
printf( "a: %s\n", aValue );
const char* bValue = b->IntToString( 42 );
printf( "b: %s\n", bValue );
printf( "a: %s\n", aValue );
return 0;
}
我得到的输出是:
a: 7
b: 42
a: 42
我原本期望第二个a:值与第一个相同。我看到编译器错误,还是我误解了lambdas和静态变量的工作方式?我是否以某种方式使用了lambda错误?
答案 0 :(得分:5)
lambda不是在需要时创建的对象,而是类的内联定义的简写。您上面的调用大致相当于:
class SomeLambda {
public:
const char* operator() (const int& i) {
static char buf[33];
sprintf(buf, "%d", i);
return buf;
}
};
...
pHasLambda->Init(SomeLambda());
静态初始化规则与成员函数的任何函数级静态具有相同的含义。
如果你有两条不同的线创建lambda ex:
auto x = []() { static char buf[99]; use_buf(buf); return buf; };
auto y = []() { static char buf[99]; use_buf(buf); return buf; };
然后,尽管定义相同,x和y将是单独的类。