I'm trying to initialize some C++ array at compile time but I got a weird g++ error. Here is the smallest chunk of code I've been able to get which reproduce the error:
#include <array>
template<typename Ar, int... Vals>
constexpr Ar Map(typename Ar::value_type /*int*/ fun(int))
{ return {{ fun(Vals)... }}; }
constexpr int add(int i) { return i + 1; }
constexpr auto b = Map<std::array<int, 2>, 1, 2>(add);
The compiler is complaining
bug.cpp:8:53: in constexpr expansion of ‘Map<std::array<int, 2ul>, {1, 2}>(add)’
bug.cpp:4:80: error: expression ‘add’ does not designate a constexpr function
constexpr Ar Map(typename Ar::value_type /*int*/ fun(int)) { return {{ fun(Vals)... }}; }
This happens both with g++ 4.7.1 and 4.9.0 20130520 (experimental). Note that
if I replace typename Ar::value_type
by int
(see the comment) in the definition of
Map
, everything works as expected. Is this a bug of am I doing something wrong ?