public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "officesamurai at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/102637] New: "Error: ‘reinterpret_cast’ is not a constant expression" when no reinterpret_cast is involved
Date: Thu, 07 Oct 2021 11:42:05 +0000	[thread overview]
Message-ID: <bug-102637-4@http.gcc.gnu.org/bugzilla/> (raw)

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) 
===

             reply	other threads:[~2021-10-07 11:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-07 11:42 officesamurai at gmail dot com [this message]
2022-02-12 22:05 ` [Bug c++/102637] " pinskia at gcc dot gnu.org
2023-11-09 19:54 ` ppalka at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-102637-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).