Consider the following code:
struct A {
constexpr operator int() { return 42; }
};
template <int>
void foo() {}
void bar(A a) {
foo<a>();
}
int main() {
foo<A{}>();
const int i = 42;
foo<i>(); // (1)
A a{};
static_assert(i == a, "");
bar(a);
foo<a>(); // error here
}
Clang 3.7 with c++14 accepts this, while gcc 5.2.0 with c++14 does not, producing the following message:
/tmp/gcc-explorer-compiler1151027-68-1f801jf/example.cpp: In function 'int main()': 26 : error: the value of 'a' is not usable in a constant expression foo<a>(); ^ 23 : note: 'a' was not declared 'constexpr' A a{}; ^ Compilation failed
Changing a
to be constexpr
as suggested by gcc fixes the gcc compilation error, but without constexpr
, which compiler is right?
For me, it seems that a
should be "usable in constant expression", as static_assert
ceritifies. Moreover, the fact that i
can be used the same way (marked (1)
), and the fact that bar()
compiles, also makes me think that gcc is wrong.
UPD: reported a bug against gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68588