【发布时间】:2017-06-22 02:38:35
【问题描述】:
在尝试在 SO 上回复 another question 时,我发现 GCC 和 clang 与 lambdas 的工作方式有所不同。
考虑以下代码:
#include <type_traits>
int main() {
int i = 0;
[j = i](){ static_assert(std::is_same<decltype(j), const int>::value, "!"); }();
}
在这种情况下,clang rejects the snippet,而 GCC accepts the code。
另一方面,他们都接受下面的代码(原因很明显):
#include <type_traits>
int main() {
int i = 0;
[j = i]()mutable{ static_assert(std::is_same<decltype(j), int>::value, "!"); }();
}
是否允许编译器将复制捕获的变量声明为非可变 lambda 的 const?
【问题讨论】:
标签: c++ c++11 lambda language-lawyer