From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23264 invoked by alias); 16 Mar 2003 22:26:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 23086 invoked by uid 71); 16 Mar 2003 22:26:00 -0000 Date: Sun, 16 Mar 2003 22:26:00 -0000 Message-ID: <20030316222600.23084.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: "Giovanni Bajo" Subject: Re: c++/10112: static data member is not correctly initialised Reply-To: "Giovanni Bajo" X-SW-Source: 2003-03/txt/msg01088.txt.bz2 List-Id: The following reply was made to PR c++/10112; it has been noted by GNATS. From: "Giovanni Bajo" To: , , , , Cc: Subject: Re: c++/10112: static data member is not correctly initialised Date: Sun, 16 Mar 2003 23:22:30 +0100 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&p r=10112 Confirmed with 3.2, cygwin on x86. Reduced snippet is: ---------------------------------- #include using namespace std; struct A { int p; A(int _p) : p(_p) {} }; template struct B { static A a; static int p1; }; template A B::a(123); template int B::p1 = a.p; int main() { // Should not assert, but it does assert(B::p1 == B::a.p); } ---------------------------------- Looking at the generated code on x86, it seems that the two variables are initialized in reversed order: 00401104 <__Z41__static_initialization_and_destruction_0ii>: [....] 401139: a1 b0 e8 40 00 mov 0x40e8b0,%eax 40113e: a3 a0 e8 40 00 mov %eax,0x40e8a0 [....] 401172: c7 04 24 b0 e8 40 00 movl $0x40e8b0,(%esp,1) 401179: c7 44 24 04 7b 00 00 movl $0x7b,0x4(%esp,1) 401180: 00 401181: e8 9a 47 00 00 call 405920 <__ZN1AC1Ei> 00405920 <__ZN1AC1Ei>: 405920: 55 push %ebp 405921: 89 e5 mov %esp,%ebp 405923: 8b 55 08 mov 0x8(%ebp),%edx 405926: 8b 45 0c mov 0xc(%ebp),%eax 405929: 89 02 mov %eax,(%edx) 40592b: 5d pop %ebp 40592c: c3 ret The code at 401139 copies B::a.p into B::p1 (it's the initialization of B::p1), while the second block initializes B::a.p with 123 (0x7b), but it's too late. I tried with no optimization, and -O0/1/2/3 and there is no difference, the code is always wrong. As for the standard, §9.4.2p7 says <>. §3.6.2p1 says: <>. Now, B::p1 is a POD type but it is not initialized with a constant expression, so I don't see any reason why it should be initialized before B::a. Giovanni Bajo