g ++(4.9)为以下代码提供了阴影警告:
class Invoke {
public:
template <typename F>
void invoke (F&& f)
{
void *p;
f(p);
}
};
int main ()
{
Invoke in;
auto fn = [](auto *p)
{
};
in.invoke(fn);
}
如果我编译它,我会在启用-Wshadow的情况下收到以下警告:
invoke.cpp: In instantiation of ‘main()::<lambda(auto:1*)> [with auto:1 = void]’:
invoke.cpp:10:12: required from ‘void Invoke::invoke(F&&) [with F = main()::<lambda(auto:1*)>&]’
invoke.cpp:22:17: required from here
invoke.cpp:18:25: warning: declaration of ‘p’ shadows a previous local [-Wshadow]
auto fn = [](auto *p)
^
invoke.cpp:8:15: note: shadowed declaration is here
void *p;
^
现在从我的角度来看,这两种情况下的变量“p”都在不同的上下文中,因此它不应该发出警告。如果我用“void *”代替“auto *”,则没有警告。这是预期的吗?