Reputation: 10657
The following code is accepted by clang and rejected by gcc. I'd like to know if it's a bug or me missing something:
#include <array>
template<typename T>
static constexpr T Apply(T in, T fun(T)) {
return fun(in);
}
template <typename T, size_t N> struct Triangle {
using Ar = std::array<T, N>;
static constexpr Ar foo(Ar line) { return line; }
static constexpr Ar results = Apply<Ar>( {{ 1 }}, foo ); // foo({1}); is ok
};
template <typename T, size_t N> constexpr std::array<T, N> Triangle<T, N>::results;
int main() {
Triangle<unsigned long, 10>::results[0];
}
GCC error message is:
binom2.cpp: In instantiation of ‘constexpr const std::array<long unsigned int, 10ul> Triangle<long unsigned int, 10ul>::results’:
binom2.cpp:16:32: required from here
binom2.cpp:11:57: in constexpr expansion of ‘Apply<std::array<long unsigned int, 10ul> >(std::array<long unsigned int, 10ul>{std::__array_traits<long unsigned int, 10ul>::_Type{1ul}}, Triangle<T, N>::foo<long unsigned int, 10ul>)’
binom2.cpp:5:16: error: expression ‘Triangle<T, N>::foo<long unsigned int, 10ul>’ does not designate a constexpr function
return fun(in);
^
Upvotes: 4
Views: 508
Reputation: 10657
This is definitely a bug. Here is the most reduced version, I manage to come with:
constexpr int Apply(int f(const int)) { return f(0); }
using Ar = int;
constexpr int id(const Ar i) { return i; }
constexpr int results1 = Apply(id);
Changing Ar
by int
in the definition of id
makes the problem disappear. So this is clearly a bug !
Upvotes: 1
Reputation: 217065
I have reduced the gcc (4.8.1) bug to the following:
#include <array>
using Ar = std::array<unsigned long, 10>;
template<typename T>
constexpr T Apply(const T& in, T (*f)(const T&)) { return f(in); }
#if 1 // if disable, bug occurs
// error: expression 'id' does not designate a constexpr function
template<>
constexpr Ar Apply<Ar>(const Ar& in, Ar (*f)(const Ar&)) { return f(in); }
#endif
constexpr Ar ApplyAr(const Ar& in, Ar (*f)(const Ar&)) { return f(in); }
static constexpr Ar id(const Ar& line) { return line; }
static constexpr Ar ar1 = {{1}};
static constexpr Ar results1 = Apply<Ar>(ar1, &id); // Fail when #if 0
static constexpr Ar results2 = ApplyAr(ar1, &id);
static constexpr Ar results3 = id(ar1);
int main() {
return 0;
}
Upvotes: 2