public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52902] New: Pointer to member rejected in constant expression
@ 2012-04-07 17:31 freunddeslichts at web dot de
  2015-09-04 14:07 ` [Bug c++/52902] " trippels at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: freunddeslichts at web dot de @ 2012-04-07 17:31 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 52902
           Summary: Pointer to member rejected in constant expression
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: freunddeslichts@web.de


#########################################################################


struct X {
  int a;
};

constexpr int X::* mem = &X::a;

void foo() 
{
  constexpr X x = {1};
  constexpr int k = x.a;          // ok

  constexpr int l = x.*mem;       // should be ok       <------------------

  //constexpr int const* m = &(x.a);  // legitimate error
}


#########################################################################

g++-4.7 -std=c++0x bug.cpp -o g47.out
bug.cpp: In function ‘void foo()’:
bug.cpp:29:24: error: ‘(const int*)(& x)’ is not a constant expression

#########################################################################


As far as I can see, a pointer to member access should be a constant
expresssion, iff both the object and the pointer are constant expressions. 
This behaviour is shown by the clang++ compiler, which does not emit the above
error.


It seems gcc evalutates the expression 

         x.*mem

as something like

         *((const int*)(& x) + mem)

In this implementation, the left (intermediate) operand to the + operator is
not a constant expression, that's why gcc rejects it by mistake.


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

end of thread, other threads:[~2015-09-04 14:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-07 17:31 [Bug c++/52902] New: Pointer to member rejected in constant expression freunddeslichts at web dot de
2015-09-04 14:07 ` [Bug c++/52902] " trippels at gcc dot gnu.org
2015-09-04 14:14 ` trippels at gcc dot gnu.org
2015-09-04 14:28 ` trippels at gcc dot gnu.org
2015-09-04 14:52 ` paolo.carlini at oracle dot com

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