c++ - 在 C++0x lambda 中是否可以看到函数局部类型定义?

标签 c++ lambda c++11 compiler-bug

我遇到了一个奇怪的问题。以下简化代码重现了 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 替换为实际类型,它会按预期工作。

这里有一些其他的测试用例:

// 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 中的一些奇怪规则,还是只是编译器中的错误?

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


目前,我已经提交了两个 bug reports .

上面的所有代码片段都应该编译。该错误与在本地定义的范围上使用范围解析有关。 (被 dvide 发现。)

而崩溃错误与...谁知道呢。 :)


更新

根据bug reports ,它们都已针对 Visual Studio 2010 的下一个版本进行了修复。(虽然这似乎不是这种情况;也许是 VS11。)

最佳答案

从 n3000,5.1.2/6,

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.

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

关于c++ - 在 C++0x lambda 中是否可以看到函数局部类型定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2122282/

相关文章:

c++ - 枚举在内部实现为整数。那为什么会报错呢?

c++ - C++ 中的迭代器

c++ - 在运行时从应用程序读取调试信息

c++ - 围绕中心点旋转对象的函数

c++ - vector vector 的快速分配

java - 在 Lambda 表达式中命名线程

amazon-web-services - DynamoDB : UpdateItem, 忽略 ExpressionAttributeValues 中的空值

c++ - xcode 4.6.3 命名空间 std 中没有名为 'minmax_element' 的成员

java - 在 Java8 中合并两个函数

当涉及 std::function 或 lambda 函数时,C++11 不推导类型