clang编译错误与static_assert和boost :: hana有关

Clang compile error related to static_assert and boost::hana

本文关键字:boost hana 有关 assert 编译 错误 static clang      更新时间:2023-10-16

考虑以下问题,该问题使用-std=c++14

#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.iid=33318.<</em>

这是Clang编译器中的一个错误,并被公认。这是它的链接:https://bugs.llvm.org/show_bug.cgi?id=33318。