【发布时间】:2017-06-04 15:31:37
【问题描述】:
考虑以下使用-std=c++14 在 Clang 3.8 上成功编译的问题。
#include <boost/hana.hpp>
namespace hana = boost::hana;
int main() {
constexpr auto indices = hana::range<unsigned, 0, 3>();
hana::for_each(indices, [&](auto i) {
hana::for_each(indices, [&](auto j) {
constexpr bool test = (i == (j == i ? j : i));
static_assert(test, "error");
});
});
}
测试很荒谬,但这不是重点。现在考虑一个替代版本,将测试直接放在static_assert 中:
#include <boost/hana.hpp>
namespace hana = boost::hana;
int main() {
constexpr auto indices = hana::range<unsigned, 0, 3>();
hana::for_each(indices, [&](auto i) {
hana::for_each(indices, [&](auto j) {
static_assert((i == (j == i ? j : i)), "error");
});
});
}
现在我得到一堆编译错误,说
错误:引用在封闭 lambda 表达式中声明的局部变量
i
问题:导致第二个版本失败的原因是什么?
编辑:这可能是编译器错误吗?我发现当在static_assert 之前访问i 时,一切都会再次编译:
#include <boost/hana.hpp>
namespace hana = boost::hana;
int main() {
constexpr auto indices = hana::range<unsigned, 0, 3>();
hana::for_each(indices, [&](auto i) {
hana::for_each(indices, [&](auto j) {
constexpr auto a = i;
static_assert((i == (j == i ? j : i)), "error");
});
});
}
更新:相同的行为可以在 Clang 4.0 和当前的开发分支 5.0 上重现。
更新 2:根据@LouisDionne 的建议,我将此作为错误提交:https://bugs.llvm.org/show_bug.cgi?id=33318。
【问题讨论】:
-
@aschepler:对不起-我不明白你的意思。您介意详细说明吗?
-
我相信这是一个编译器错误。我认为你最好的办法是针对 Clang 提交一个错误,看看那里有知识的人是怎么想的。
-
@LouisDionne - 感谢您的建议。它被确认为 Clang 中的一个错误。
-
您能否在您自己的问题的答案中发布该错误的链接,以便将其标记为已回答?
标签: c++ clang++ static-assert boost-hana