Clang compilation error related to static_assert and boost :: hana

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 pretty insensitive, but that's not the point. Now consider an alternative version where the test is directly placed inside 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 compilation errors saying

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

Question: what causes the second version to crash?

Edit: Could this be a compiler error? I get that when i 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 behavior can be reproduced on Clang 4.0 and the current development branch 5.0.

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

+5
source share
1 answer

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

+1
source

Source: https://habr.com/ru/post/1268530/


All Articles