This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/52892] Function pointer loses constexpr qualification
- From: "daniel.kruegler at googlemail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sat, 07 Apr 2012 13:15:13 +0000
- Subject: [Bug c++/52892] Function pointer loses constexpr qualification
- Auto-submitted: auto-generated
- References: <bug-52892-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52892
--- Comment #1 from Daniel KrÃgler <daniel.kruegler at googlemail dot com> 2012-04-07 13:15:13 UTC ---
(In reply to comment #0)
[..]
> Based on my reading of the standard, this should be allowed behavior, and
> works as expected with clang 3.1 (152539).
I agree that this should work, this was the clear intention for the core
language defect
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1193
I tried to break down the example to understand what's going wrong and here is
one simpler example:
//---------------
constexpr bool is_negative(int x) { return x < 0; }
struct Defer
{
#if 0
typedef bool (*Function)(int);
Function func;
constexpr Defer(Function func) : func(func) {}
#else
bool (*func)(int);
constexpr Defer(bool (* func)(int)) : func(func) {}
#endif
template<class... Args>
constexpr auto operator()(const Args&... args) -> decltype(func(args...))
{
return func(args...);
}
};
template<class Function>
constexpr Defer make_deferred(Function func)
{
return Defer(func);
}
int main()
{
constexpr Defer deferred(make_deferred(is_negative));
static_assert(deferred(-2), "Error");
}
//---------------
As written, this example is well-formed. But once we change the pre-processor
directive "#if 0" to "#if 1", we have a similar error. It seems that after
introduction of the typedef for the function pointer type gcc no longer
attempts to consider the track the constness.
It is possible to construct an even simpler example. Consider the code example
from CWG defect 1193 again:
constexpr bool is_negative(int x) { return x < 0; }
constexpr bool check(int x, bool (*p)(int)) { return p(x); }
static_assert(check(-2, is_negative), "Error");
gcc accepts it as it should. Now lets introduce a typedef for the function
pointer used in check:
constexpr bool is_negative(int x) { return x < 0; }
typedef bool (*Function)(int);
constexpr bool check(int x, Function p) { return p(x); }
static_assert(check(-2, is_negative), "Error");
Now we get a similar error as in your example:
"4|error: non-constant condition for static assertion|
4| in constexpr expansion of 'check(-2, is_negative)'|
3|error: expression 'is_negative' does not designate a constexpr function"
The template parameter in your example has similar effects as a typedef. Both
use cases should not invalidate the constexpr character.