https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68588
Bug ID: 68588
Summary: GCC requires constexpr for template non-type
parameter, even though constexpr conversion operator
exists
Product: gcc
Version: 5.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: petr at kalinin dot nnov.ru
Target Milestone: ---
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 (by "g++
--std=c++14 b.cpp -o b") does not, producing the following message:
b.cpp: In function ‘int main()’:
b.cpp:22:9: error: the value of ‘a’ is not usable in a constant expression
foo<a>(); // error here
^
b.cpp:18:7: note: ‘a’ was not declared ‘constexpr’
A a{};
^
This behavior of GCC seems at at least strange, as it allows "a" in
static_assert, as well in bar(), but not directly in foo<a>. Moreover,
A::operator int() is declared constexpr and does not evaluate any class
members, therefore use of "a" should be converted constant expression.
----
This is from my question
http://stackoverflow.com/questions/33957274/type-conversion-at-template-non-type-argument-without-constexpr/33958291.
The last sentence here is reworded from Serge Ballesta's answer there.