This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/52892] New: Function pointer loses constexpr qualification


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52892

             Bug #: 52892
           Summary: Function pointer loses constexpr qualification
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: EricMCornelius@gmail.com


The following test case is failing:

#include <cstdio>

constexpr std::size_t fibonacci(std::size_t val) {
  return (val <= 2) ? 1 : fibonacci(val - 1) + fibonacci(val - 2);
}

template <typename Function>
struct Defer {
  constexpr Defer(const Function func_) : func(func_) { }

  const Function func;

  template <typename... Args>
  constexpr auto operator () (const Args&... args) -> decltype(func(args...)) {
    return func(args...);
  }
};

template <typename Function>
constexpr Defer<Function> make_deferred(const Function f) {
  return Defer<Function>(f);
}

int main(int argc, char* argv[]) {
  constexpr auto deferred = make_deferred(&fibonacci);
  static_assert(deferred(25) == 75025, "Static fibonacci call failed");
}

src/main.cpp: In function 'int main(int, char**)':
src/main.cpp:151:3: error: non-constant condition for static assertion
src/main.cpp:151:28:   in constexpr expansion of
'deferred.Defer<Function>::operator()<{int}>((* &25))'
src/main.cpp:140:24: error: expression 'fibonacci' does not designate a
constexpr function
test.make:129: recipe for target `obj/Debug/main.o' failed
make[1]: *** [obj/Debug/main.o] Error 1
makefile:16: recipe for target `test' failed
make: *** [test] Error 2

Based on my reading of the standard, this should be allowed behavior, and works
as expected with clang 3.1 (152539).

Note that the following behavior also fails similarly:

int main(int argc, char* argv[]) {
  constexpr auto deferred = make_deferred(&fibonacci);
  constexpr auto func = deferred.func;
  constexpr auto val = func(25);
}

src/main.cpp: In function 'int main(int, char**)':
src/main.cpp:152:31: error: expression 'fibonacci' does not designate a
constexpr function

Whereas this succeeds:

int main(int argc, char* argv[]) {
  constexpr auto func = &fibonacci;
  static_assert(func(25) == 75025, "Static fibonacci call failed");
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]