msvc 2017中的错误C3489错误?

时间:2018-03-09 02:44:55

标签: c++ lambda visual-studio-2017

这段代码应该编译吗?

int main()
{
    int a, b;
    [=, b]{ return a; };

    return 0;
}

msvc 2017版15.6.1给了我一个错误:

error C3489: '&b' is required when the default capture mode is by copy (=)

相同的代码在gcc 6.3上编译正常:https://ideone.com/HzdiJw

Lightness Races in Orbit评论的解决方法:[=, b_=b]{ return a; };

1 个答案:

答案 0 :(得分:1)

这在C ++ 14中是非法的:

  

[C++14: 5.1.2/8]: [..] 如果 lambda-capture 包含 capture-default ,即= lambda-capture 的每个 simple-capture 应为“& identifier”形式。 [..]

仍然在the current working draft

  

[N4727: 8.4.5.2/2]: [..] 如果 lambda-capture 包含 capture-default = =,则 > lambda-capture 的简单捕获的格式应为“& identifier”,“this”或“* this”。 [..]

它不太可能在C ++中变得合法17然后再次变得非法。

此外,GCC 6.3 does complain about this code,因此ideone.com环境正在构建时没有-Werror,然后报告成功构建而不传递警告。

相关问题