public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/114031] New: gcc rejects valid code with pointer to member field cast inside a class not completed
@ 2024-02-21 13:06 rush102333 at gmail dot com
  2024-02-21 15:12 ` [Bug c++/114031] " mpolacek at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: rush102333 at gmail dot com @ 2024-02-21 13:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114031

            Bug ID: 114031
           Summary: gcc rejects valid code with pointer to member field
                    cast inside a class not completed
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Keywords: diagnostic, rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rush102333 at gmail dot com
  Target Milestone: ---

The following code is rejected by gcc 13 ,which does not trigger any error in
both clang and MSVC:
https://godbolt.org/z/5T7sx8vs8:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<iostream>
struct A { int a; };
struct B:A { 
  int y=0;
  constexpr static bool b=(int(A::*))&B::y; 
};

int main(){
    std::cout<<B::b<<std::endl;
    return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The compiler complains that &B::y can't be evaluated as constant when class B
is still incomplete:


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:5:42: error: '&B::y' is not a constant expression when the class 'B'
is still incomplete
    5 |   constexpr static bool b=(int(A::*))&B::y;
      |                                          ^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Note that merely using &B::y without the member pointer conversion
expression(int (A:: *)) does not trigger any error:
https://godbolt.org/z/7oErdvKv8. 
If it's &B::y that indeed causes the error, that shouldn't compile either.

This looks somewhat similar to PR107574. At least the error message needs
further improvement.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/114031] gcc rejects valid code with pointer to member field cast inside a class not completed
  2024-02-21 13:06 [Bug c++/114031] New: gcc rejects valid code with pointer to member field cast inside a class not completed rush102333 at gmail dot com
@ 2024-02-21 15:12 ` mpolacek at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-02-21 15:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114031

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
// PR c++/114031

struct A { int a; };
struct B:A {
  int y = 0;
  constexpr static bool b = (int A::*) &B::y;
};

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-21 15:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-21 13:06 [Bug c++/114031] New: gcc rejects valid code with pointer to member field cast inside a class not completed rush102333 at gmail dot com
2024-02-21 15:12 ` [Bug c++/114031] " mpolacek at gcc dot gnu.org

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