2

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 ?

hivert
  • 10,579
  • 3
  • 31
  • 56

1 Answers1

5

I think this is Bug 52892 - Function pointer loses constexpr qualification .

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • It does compile when `typename Ar::value_type` is replaced with `int` though – mirk Jul 12 '13 at 09:25
  • @mirk: Read the bug report: `The template parameter in your example has similar effects as a typedef. Both use cases should not invalidate the constexpr character.` It happens when using with a `typedef` or template parameter. – Jesse Good Jul 12 '13 at 09:27
  • Thank for the answer ! Debugging those code is still a nightmare. – hivert Jul 12 '13 at 10:25