Clang compile error related to static_assert and boost::hana

孤者浪人 提交于 2019-12-10 13:45:28

问题


Consider the following problem which compiles successfully on Clang 3.8 using -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");
        });
    });
}

The test is quite non-sensical but that is not the point. Consider now an alternative version where the test is directly put inside the 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");
        });
    });
}

Now I get a bunch of compile errors, saying

error: reference to local variable i declared in enclosing lambda expression

Question: What causes the second version to fail?

Edit: Could this be a compiler bug? I turns out that when accessing i before the static_assert, everything compiles again:

#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");
        });
    });
}

Update: The same behaviour can be reproduced on Clang 4.0 and the current development branch 5.0.

Update 2: As suggested by @LouisDionne, I filed this as a bug: https://bugs.llvm.org/show_bug.cgi?id=33318.


回答1:


This is a bug in the Clang compiler and was acknowledged as such. Here is the link to it: https://bugs.llvm.org/show_bug.cgi?id=33318.



来源:https://stackoverflow.com/questions/44355720/clang-compile-error-related-to-static-assert-and-boosthana

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!