[英]Expected behaviour with c++ lambdas and static variables
I'm using VS2013, and discovered what appears to me to be strange behaviour when using multiple instances of a class that contains lambdas, and those lambdas contain static variables. 我正在使用VS2013,发现使用包含lambda的类的多个实例且这些lambda包含静态变量时,对我来说似乎是奇怪的行为。 The static variables appear to be shared.
静态变量似乎是共享的。
Example code, very much trimmed down but still captures the essence: 示例代码已被精简,但仍能捕捉本质:
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;
}
The output I get is: 我得到的输出是:
a: 7
b: 42
a: 42
I would have expected the second a: value to be the same as the first. 我希望第二个a:值与第一个相同。 Am I seeing a compiler bug, or am I misunderstanding the way lambdas and static variables therein work?
我看到编译器错误了吗,还是误解了其中的lambda和静态变量的工作方式? Am I using the lambda wrong in some way?
我在某种程度上使用lambda是否出错?
A lambda is not an object that is created when needed, but a shorthand for an inline definition of a class. Lambda不是在需要时创建的对象,而是类的内联定义的简写。 Your invocation above would be roughly equivalent to:
您上面的调用大致相当于:
class SomeLambda {
public:
const char* operator() (const int& i) {
static char buf[33];
sprintf(buf, "%d", i);
return buf;
}
};
...
pHasLambda->Init(SomeLambda());
The static initialization rules there have the same meaning as any function level static for a member function. 静态初始化规则与成员函数的任何静态函数级具有相同的含义。
If you instead had two different lines creating the lambda ex: 如果您改用两行代码来创建lambda ex:
auto x = []() { static char buf[99]; use_buf(buf); return buf; };
auto y = []() { static char buf[99]; use_buf(buf); return buf; };
Then x and y would be separate classes despite having identical definitions. 这样,尽管x和y的定义相同,但它们将是单独的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.