[Bug c++/102637] New: "Error: ‘reinterpret_cast’ is not a constant expression" when no reinterpret_cast is involved
officesamurai at gmail dot com
gcc-bugzilla@gcc.gnu.org
Thu Oct 7 11:42:05 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102637
Bug ID: 102637
Summary: "Error: ‘reinterpret_cast’ is not a constant
expression" when no reinterpret_cast is involved
Product: gcc
Version: 11.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: officesamurai at gmail dot com
Target Milestone: ---
Here I obtain a function pointer to the same member function two times, through
the base and through the derived class. Then I try to compare the pointers in a
constexpr context and it fails with the error "‘reinterpret_cast’ is not a
constant expression". The same happens when I try to static_cast each pointer
to the opposite type.
gcc_constexpr_funcptr_issue.cpp:
===
struct B1
{
void foo(int) {}
};
struct B2
{
void foo(bool) {}
};
struct D: B1
#ifndef ONE_BASE
, B2
#endif
{
using B1::foo;
#ifndef ONE_BASE
using B2::foo;
#endif
};
template <typename Class, typename Param>
constexpr auto select(void (Class::*func)(Param))
{
return func;
}
int main()
{
constexpr auto bFunc = select<B1, int>(&B1::foo);
constexpr auto dFunc = select<D, int>(&D::foo);
static_assert(bFunc == dFunc, "");
constexpr auto bFuncCastToDFunc = static_cast<void (D::*)(int)>(bFunc);
constexpr auto dFuncCastToBFunc = static_cast<void (B1::*)(int)>(dFunc);
}
===
Compiler invocation:
===
$ g++-11.2.0 -c gcc_constexpr_funcptr_issue.cpp
gcc_constexpr_funcptr_issue.cpp: In function ‘int main()’:
gcc_constexpr_funcptr_issue.cpp:33:25: error: non-constant condition for static
assertion
33 | static_assert(bFunc == dFunc, "");
| ~~~~~~^~~~~~~~
gcc_constexpr_funcptr_issue.cpp:33:28: error: ‘reinterpret_cast’ is not a
constant expression
33 | static_assert(bFunc == dFunc, "");
| ^~~~~
gcc_constexpr_funcptr_issue.cpp:35:39: error: ‘reinterpret_cast’ is not a
constant expression
35 | constexpr auto bFuncCastToDFunc = static_cast<void
(D::*)(int)>(bFunc);
|
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gcc_constexpr_funcptr_issue.cpp:36:39: error: ‘reinterpret_cast’ is not a
constant expression
36 | constexpr auto dFuncCastToBFunc = static_cast<void
(B1::*)(int)>(dFunc);
|
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
===
Additionally, if D has only one base, the call select<D, int>(&D::foo) also
fails:
---
$ g++-11.2.0 -DONE_BASE -c gcc_constexpr_funcptr_issue.cpp
gcc_constexpr_funcptr_issue.cpp: In function ‘int main()’:
gcc_constexpr_funcptr_issue.cpp:31:42: error: ‘reinterpret_cast’ is not a
constant expression
31 | constexpr auto dFunc = select<D, int>(&D::foo);
| ~~~~~~~~~~~~~~^~~~~~~~~
<skipped>
===
Finally, even if I replace the definitions of bFunc and dFunc with
constexpr auto bFunc = &B1::foo;
constexpr auto dFunc = &D::foo;
and use -DONE_BASE, one of the casts still fails:
===
$ g++-11.2.0 -DONE_BASE -c gcc_constexpr_funcptr_issue.cpp
gcc_constexpr_funcptr_issue.cpp: In function ‘int main()’:
gcc_constexpr_funcptr_issue.cpp:35:39: error: ‘reinterpret_cast’ is not a
constant expression
35 | constexpr auto bFuncCastToDFunc = static_cast<void
(D::*)(int)>(bFunc);
|
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
===
Compiler info:
===
$ g++-11.2.0 -v
Using built-in specs.
COLLECT_GCC=g++-11.2.0
COLLECT_LTO_WRAPPER=/home/brd/soft/gcc-11.2.0/libexec/gcc/x86_64-pc-linux-gnu/11.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --prefix=/home/brd/soft/gcc-11.2.0
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (GCC)
===
More information about the Gcc-bugs
mailing list