Clang编译与static_assert和boost :: hana相关的错误
问题描述:
考虑以下问题,它使用-std=c++14
在Clang 3.8上成功编译。Clang编译与static_assert和boost :: hana相关的错误
#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");
});
});
}
现在,我得到了一堆编译错误,说
error: reference to local variable
i
declared in enclosing lambda expression
问:是什么原因导致了第二个版本失败?
编辑:这可能是一个编译器错误?我原来的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的建议,我将此作为bug提交:https://bugs.llvm.org/show_bug.cgi?id=33318。
@aschepler:对不起 - 我不明白你的意思。你介意阐述吗? – LocalVolatility
“这个”是类似的https://stackoverflow.com/questions/43665610/why-is-this-nested-lambda-not-considered-constexpr –
我_believe_这是一个编译器错误。我认为你最好的选择是提交一份反对Clang的错误报告,看看那里的知识渊博的人们在想什么。 –