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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With