From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2557 invoked by alias); 12 May 2007 15:53:14 -0000 Received: (qmail 2531 invoked by alias); 12 May 2007 15:53:04 -0000 Date: Sat, 12 May 2007 15:53:00 -0000 Message-ID: <20070512155304.2530.qmail@sourceware.org> X-Bugzilla-Reason: CC References: Subject: [Bug c++/31904] fail to link to static const double In-Reply-To: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "fang at csl dot cornell dot edu" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2007-05/txt/msg00903.txt.bz2 ------- Comment #5 from fang at csl dot cornell dot edu 2007-05-12 16:53 ------- Subject: Re: fail to link to static const double > ------- Comment #3 from dennis0yang at gmail dot com 2007-05-12 09:29 ------- > Subject: Re: fail to link to static const double > > I understand that the standard only specifies that static const for > integral type can be assigned within the class. My point is that if gcc > decides to allow the extension of assigning static const double within > the class (good extension, I don't see why this is not in the > standard.), then it should allow me to link to it. Note the code works > if I change "y=-x" to "y= -1 * x", so it is clearly a bug. To *link* to something correctly with a reference, you need EXACTLY ONE definition, same as in C. An in-class declaration is not a definition. It cannot be treated as such because you'd end up with a definition in each translation unit that includes the header (multiply defined). The cases in which the compile/link 'worked' for you were cases in which the compiler substituted the value for the reference (you got lucky). I don't know the heuristics for the decision, maybe ask someone familiar with the constant-folding opt. > I also tried your suggestion before submitting the bug report. It does > work. But I really do not want to create a .cpp file just to put that > one line in it, much prefer to leave everything in the .h file. Defining it once in a .cpp file is the correct thing to do, even if it is inconvenient. Hope this helps. > fang at csl dot cornell dot edu wrote: > > ------- Comment #1 from fang at csl dot cornell dot edu 2007-05-12 09:06 ------- > > You need to define Base::x out of class in some translation unit, the in-class > > declaration alone isn't enough, though sometimes the compiler will elide the > > reference to it if its value is known (optimization). > > > > const double Base::x = 1.0; -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31904