【发布时间】:2011-12-11 06:47:31
【问题描述】:
下面的例子演示了我在VC++ 2010中遇到的问题:
class Foo
{
template<class T>
static T foo(T t) { return t; }
public:
void test()
{
auto lambda = []() { return foo(1); }; // call to Foo::foo<int>
lambda();
}
};
VC++ 产生:error C3861: 'foo': identifier not found
如果我限定了对 foo:Foo::foo(1); 的调用,那么这个示例编译时会出现警告:
警告 C4573:'Foo::foo' 的使用要求编译器捕获 'this',但当前默认捕获模式不允许
标准对这个案例有什么看法?是否应该找到不合格的名称?限定名称是否需要捕获this?
【问题讨论】:
-
嗯,这很奇怪。它是静态的,因此不需要
this。我怀疑这是编译器错误,没有找到它是编译器认为它需要this才能使用它的副作用。 -
g++ 也会发生这种情况,所以它看起来不像是一个错误。如果将其更改为 [&]() 而不是 []() 则编译正常,但这是因为它通过引用获取 this。
-
话说another thread 说这是一个错误。