public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/31904]  New: fail to link to static const double
@ 2007-05-12  1:41 dennis0yang at gmail dot com
  2007-05-12  8:06 ` [Bug c++/31904] " fang at csl dot cornell dot edu
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: dennis0yang at gmail dot com @ 2007-05-12  1:41 UTC (permalink / raw)
  To: gcc-bugs

The following code fail to compile under cygwin. The relevant error message is:
...: undefined reference to `Base::x'
collect2: ld returned 1 exit status
However if I change the statement "y=-x" to "y=x", then it compiles fine.


class Base
{
  public:
    virtual void f () = 0;
    static const double x = 1;
};

class Derived : public Base
{
  public:
    Derived () {};
    void f () { double y = -x; };
};

int main()
{
  Derived foo;
  return 0;
}
~


-- 
           Summary: fail to link to static const double
           Product: gcc
           Version: 3.4.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dennis0yang at gmail dot com


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


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

* [Bug c++/31904] fail to link to static const double
  2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
@ 2007-05-12  8:06 ` fang at csl dot cornell dot edu
  2007-05-12  8:15 ` schwab at suse dot de
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-05-12  8:06 UTC (permalink / raw)
  To: gcc-bugs



------- 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;


-- 

fang at csl dot cornell dot edu changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fang at csl dot cornell dot
                   |                            |edu


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


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

* [Bug c++/31904] fail to link to static const double
  2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
  2007-05-12  8:06 ` [Bug c++/31904] " fang at csl dot cornell dot edu
@ 2007-05-12  8:15 ` schwab at suse dot de
  2007-05-12  8:29 ` dennis0yang at gmail dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: schwab at suse dot de @ 2007-05-12  8:15 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from schwab at suse dot de  2007-05-12 09:15 -------


*** This bug has been marked as a duplicate of 30745 ***


-- 

schwab at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


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


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

* [Bug c++/31904] fail to link to static const double
  2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
  2007-05-12  8:06 ` [Bug c++/31904] " fang at csl dot cornell dot edu
  2007-05-12  8:15 ` schwab at suse dot de
@ 2007-05-12  8:29 ` dennis0yang at gmail dot com
  2007-05-12  8:33 ` dennis0yang at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: dennis0yang at gmail dot com @ 2007-05-12  8:29 UTC (permalink / raw)
  To: gcc-bugs



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

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.

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


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

* [Bug c++/31904] fail to link to static const double
  2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
                   ` (2 preceding siblings ...)
  2007-05-12  8:29 ` dennis0yang at gmail dot com
@ 2007-05-12  8:33 ` dennis0yang at gmail dot com
  2007-05-12 15:53 ` fang at csl dot cornell dot edu
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: dennis0yang at gmail dot com @ 2007-05-12  8:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from dennis0yang at gmail dot com  2007-05-12 09:33 -------
Subject: Re:  fail to link to static const double

I just checked out the bug report you mentioned, are you sure they are 
the same? When I change "static const double" to "static const int", the 
code compiles without any problem. It also compiles when I replace the 
statement "y=-x" with "y= -1 * x". So maybe it is a different bug.

schwab at suse dot de wrote:
> ------- Comment #2 from schwab at suse dot de  2007-05-12 09:15 -------
>
>
> *** This bug has been marked as a duplicate of 30745 ***
>
>
>   


-- 


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


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

* [Bug c++/31904] fail to link to static const double
  2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
                   ` (3 preceding siblings ...)
  2007-05-12  8:33 ` dennis0yang at gmail dot com
@ 2007-05-12 15:53 ` fang at csl dot cornell dot edu
  2007-05-12 16:19 ` dennis0yang at gmail dot com
  2007-05-12 16:53 ` fang at csl dot cornell dot edu
  6 siblings, 0 replies; 8+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-05-12 15:53 UTC (permalink / raw)
  To: gcc-bugs



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


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

* [Bug c++/31904] fail to link to static const double
  2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
                   ` (4 preceding siblings ...)
  2007-05-12 15:53 ` fang at csl dot cornell dot edu
@ 2007-05-12 16:19 ` dennis0yang at gmail dot com
  2007-05-12 16:53 ` fang at csl dot cornell dot edu
  6 siblings, 0 replies; 8+ messages in thread
From: dennis0yang at gmail dot com @ 2007-05-12 16:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from dennis0yang at gmail dot com  2007-05-12 17:19 -------
Subject: Re:  fail to link to static const double

I understand perfectly well everything you said. But I think you miss 
the point of this bug report.
This is not about the c++ standard in terms of what should and should 
not be done. This is about get rid of the "luck" component in gcc. If 
gcc decides not to support in-class definition for static const double, 
then flag the statement as an error. I really don't see why the compile 
doesn't substitute the value everywhere it see the reference (as you 
mentioned), because it is a static const!

fang at csl dot cornell dot edu wrote:
> ------- 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


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

* [Bug c++/31904] fail to link to static const double
  2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
                   ` (5 preceding siblings ...)
  2007-05-12 16:19 ` dennis0yang at gmail dot com
@ 2007-05-12 16:53 ` fang at csl dot cornell dot edu
  6 siblings, 0 replies; 8+ messages in thread
From: fang at csl dot cornell dot edu @ 2007-05-12 16:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from fang at csl dot cornell dot edu  2007-05-12 17:53 -------
Subject: Re:  fail to link to static const double

> ------- Comment #6 from dennis0yang at gmail dot com  2007-05-12 17:19 -------
> Subject: Re:  fail to link to static const double
>
> I understand perfectly well everything you said. But I think you miss
> the point of this bug report.
> This is not about the c++ standard in terms of what should and should
> not be done. This is about get rid of the "luck" component in gcc. If
> gcc decides not to support in-class definition for static const double,
> then flag the statement as an error. I really don't see why the compile

You can reject non-integer in-class static const definitions with:
CXXFLAGS += -ansi -pedantic-errors
(also rejects many other non-standardisms, if you're prepared to do so!)

> doesn't substitute the value everywhere it see the reference (as you
> mentioned), because it is a static const!

The compiler is really not required to do even the simplest of
optimizations.  -O0 really means NO optimizations, even the no-brainers
such as this one.

FWIW, as a user, I build/test with different optimization levels to catch
these sort of errors (in case I forget a const definition).


-- 


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


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

end of thread, other threads:[~2007-05-12 16:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-12  1:41 [Bug c++/31904] New: fail to link to static const double dennis0yang at gmail dot com
2007-05-12  8:06 ` [Bug c++/31904] " fang at csl dot cornell dot edu
2007-05-12  8:15 ` schwab at suse dot de
2007-05-12  8:29 ` dennis0yang at gmail dot com
2007-05-12  8:33 ` dennis0yang at gmail dot com
2007-05-12 15:53 ` fang at csl dot cornell dot edu
2007-05-12 16:19 ` dennis0yang at gmail dot com
2007-05-12 16:53 ` fang at csl dot cornell dot edu

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