在C ++ 0x Lambda内部可见函数局部typedef吗? [英] Are function-local typedefs visible inside C++0x lambdas?

查看:67
本文介绍了在C ++ 0x Lambda内部可见函数局部typedef吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题。下面的简化代码重现了MSVC 2010中的问题:

I've run into a strange problem. The following simplified code reproduces the problem in MSVC 2010:

template <typename T>
struct dummy
{
    static T foo(void) { return T(); }
};

int main(void)
{
    typedef dummy<bool> dummy_type;
    auto x = []{ bool b = dummy_type::foo(); };
    // auto x = []{ bool b = dummy<bool>::foo(); }; // works
}

我创建的 typedef 在函数中本地显示在lambda中似乎不可见。如果我将 typedef 替换为实际类型,它将按预期工作。

The typedef I created locally in the function doesn't seem to be visible in the lambda. If I replace the typedef with the actual type, it works as expected.

以下是一些其他测试用例:

Here are some other test cases:

// crashes the compiler, credit to Tarydon
int main(void)
{
    struct dummy {};

    auto x = []{ dummy d; };
}

// works as expected
int main(void)
{
    typedef int integer;

    auto x = []{ integer i = 0; };
}

我暂时没有g ++可以对其进行测试。这是C ++ 0x中的一条奇怪规则,还是只是编译器中的错误?

I don't have g++ available to test it, right now. Is this some strange rule in C++0x, or just a bug in the compiler?

从上面的结果来看,我倾向于错误。虽然崩溃绝对是一个错误。

From the results above, I'm leaning towards bug. Though the crash is definitely a bug.

目前,我已经提交了两个错误报告

For now, I have filed two bug reports.

上面的所有代码段均应编译。该错误与在本地定义的范围上使用范围解析有关。 (由 dvide 察觉。)

All code snippets above should compile. The error has to do with using the scope resolution on locally defined scopes. (Spotted by dvide.)

与崩溃错误有关。 .. 谁知道。 :)

And the crash bug has to do with... who knows. :)

根据错误报告,它们都已在下一版Visual Studio 2010中得到修复。

According to the bug reports, they have both been fixed for the next release of Visual Studio 2010. (Though this doesn't seem to be the case; VS11 perhaps.)

推荐答案

从n3000,5.1.2 / 6,

From n3000, 5.1.2/6,


lambda表达式的
复合语句产生函数
调用运算符的
函数体(8.4),但是出于
名称查找(3.4)的目的,…
复合语句在
中被视为lambda表达式的上下文。

The lambda-expression’s compound-statement yields the function-body (8.4) of the function call operator, but for purposes of name lookup (3.4), … the compound-statement is considered in the context of the lambda-expression.

毫不奇怪,本地类型应该可见。

Not surprisingly, the local type should be visible.

这篇关于在C ++ 0x Lambda内部可见函数局部typedef吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆