public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/42330]  New: undefined reference to "static const int" in class when passing as "const int &" to a function
@ 2009-12-08  4:31 aijunbai at gmail dot com
  2009-12-08  9:18 ` [Bug c++/42330] " paolo dot carlini at oracle dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: aijunbai at gmail dot com @ 2009-12-08  4:31 UTC (permalink / raw)
  To: gcc-bugs

consider the flowing code:

struct A {
    static const int i = 0;
};

void foo(int)
{
}

void bar(const int &)
{
}

int main() {
    foo(A::i);
    bar(A::i);

    return 0;
}

when compiling with -O0 option, g++ reports that: undefined reference to
`A::i',
but it will be ok if i use -O3 option.

gcc -v says:
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu8'
--with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--enable-multiarch --enable-linker-build-id --with-system-zlib
--libexecdir=/usr/lib --without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls
--enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc
--enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu
--target=i486-linux-gnu
Thread model: posix
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)


-- 
           Summary: undefined reference to "static const int" in class when
                    passing as "const int &" to a function
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: aijunbai at gmail dot com


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
@ 2009-12-08  9:18 ` paolo dot carlini at oracle dot com
  2009-12-08  9:47 ` aijunbai at gmail dot com
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-12-08  9:18 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from paolo dot carlini at oracle dot com  2009-12-08 09:18 -------
You aren't defining anywhere A::i, just add, after struct A:

  const int A::i;

and things will be fine.

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


-- 

paolo dot carlini at oracle dot com changed:

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


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
  2009-12-08  9:18 ` [Bug c++/42330] " paolo dot carlini at oracle dot com
@ 2009-12-08  9:47 ` aijunbai at gmail dot com
  2009-12-08  9:49 ` redi at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: aijunbai at gmail dot com @ 2009-12-08  9:47 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from aijunbai at gmail dot com  2009-12-08 09:46 -------
(In reply to comment #1)
> You aren't defining anywhere A::i, just add, after struct A:
> 
>   const int A::i;
> 
> and things will be fine.
> 
> *** This bug has been marked as a duplicate of 42101 ***
> 

thanks for your reply.

i found that no errors will be reported if i delete the line `bar(A::i)', so is
that a bug?

in fact the original code is like:
template <int>
class A
{
    static const int i = -1;
}

template <>
class A<0>
{
    static const int i = 0;
}

//and some other specializations...

and i don't want to write lots of "const int A<N>::i"...


-- 

aijunbai at gmail dot com changed:

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


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
  2009-12-08  9:18 ` [Bug c++/42330] " paolo dot carlini at oracle dot com
  2009-12-08  9:47 ` aijunbai at gmail dot com
@ 2009-12-08  9:49 ` redi at gcc dot gnu dot org
  2009-12-08  9:50 ` redi at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu dot org @ 2009-12-08  9:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from redi at gcc dot gnu dot org  2009-12-08 09:48 -------
(In reply to comment #2)
> and i don't want to write lots of "const int A<N>::i"...

You have to, your code is not valid if you use A::i in the program but it has
no definition.  This is not a compiler bug.




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


-- 

redi at gcc dot gnu dot org changed:

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


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
                   ` (2 preceding siblings ...)
  2009-12-08  9:49 ` redi at gcc dot gnu dot org
@ 2009-12-08  9:50 ` redi at gcc dot gnu dot org
  2009-12-08 11:52 ` aijunbai at gmail dot com
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu dot org @ 2009-12-08  9:50 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from redi at gcc dot gnu dot org  2009-12-08 09:50 -------
(In reply to comment #3)
> (In reply to comment #2)
> > and i don't want to write lots of "const int A<N>::i"...
> 
> You have to, your code is not valid if you use A::i in the program but it has
> no definition.  This is not a compiler bug.

Actually you don't have to write lots, just one:

template<int N> const A<N>::i;


-- 


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
                   ` (3 preceding siblings ...)
  2009-12-08  9:50 ` redi at gcc dot gnu dot org
@ 2009-12-08 11:52 ` aijunbai at gmail dot com
  2009-12-08 12:23 ` redi at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: aijunbai at gmail dot com @ 2009-12-08 11:52 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from aijunbai at gmail dot com  2009-12-08 11:52 -------
(In reply to comment #4)
> (In reply to comment #3)
> > (In reply to comment #2)
> > > and i don't want to write lots of "const int A<N>::i"...
> > 
> > You have to, your code is not valid if you use A::i in the program but it has
> > no definition.  This is not a compiler bug.
> 
> Actually you don't have to write lots, just one:
> 
> template<int N> const A<N>::i;
> 

I tried so, but it seems do not work, could you please explain more detailedly?
thx~


-- 


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
                   ` (4 preceding siblings ...)
  2009-12-08 11:52 ` aijunbai at gmail dot com
@ 2009-12-08 12:23 ` redi at gcc dot gnu dot org
  2009-12-08 13:02 ` aijunbai at gmail dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu dot org @ 2009-12-08 12:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from redi at gcc dot gnu dot org  2009-12-08 12:23 -------
(In reply to comment #5)
> > 
> > template<int N> const A<N>::i;
> > 
> 
> I tried so, but it seems do not work, could you please explain more detailedly?
> thx~

I missed the "int" out:

template<int N> const int A<N>::i;


-- 


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
                   ` (5 preceding siblings ...)
  2009-12-08 12:23 ` redi at gcc dot gnu dot org
@ 2009-12-08 13:02 ` aijunbai at gmail dot com
  2009-12-08 13:13 ` paolo dot carlini at oracle dot com
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: aijunbai at gmail dot com @ 2009-12-08 13:02 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from aijunbai at gmail dot com  2009-12-08 13:02 -------
(In reply to comment #6)
> (In reply to comment #5)
> > > 
> > > template<int N> const A<N>::i;
> > > 
> > 
> > I tried so, but it seems do not work, could you please explain more detailedly?
> > thx~
> 
> I missed the "int" out:
> 
> template<int N> const int A<N>::i;
> 

thanks for your help, but it still can not be compiled under gcc 4.4.1


-- 


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
                   ` (6 preceding siblings ...)
  2009-12-08 13:02 ` aijunbai at gmail dot com
@ 2009-12-08 13:13 ` paolo dot carlini at oracle dot com
  2009-12-08 14:06 ` aijunbai at gmail dot com
  2009-12-08 14:15 ` paolo dot carlini at oracle dot com
  9 siblings, 0 replies; 11+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-12-08 13:13 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from paolo dot carlini at oracle dot com  2009-12-08 13:12 -------
Then show here exactly what you are trying to compile. Note: this is *not*
gcc-help.


-- 


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
                   ` (7 preceding siblings ...)
  2009-12-08 13:13 ` paolo dot carlini at oracle dot com
@ 2009-12-08 14:06 ` aijunbai at gmail dot com
  2009-12-08 14:15 ` paolo dot carlini at oracle dot com
  9 siblings, 0 replies; 11+ messages in thread
From: aijunbai at gmail dot com @ 2009-12-08 14:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from aijunbai at gmail dot com  2009-12-08 14:06 -------
(In reply to comment #8)
> Then show here exactly what you are trying to compile. Note: this is *not*
> gcc-help.
> 

alright, take the flowing code as an example:

template<int>
struct A {
    static const int i = -1;
};

template<>
struct A<0> {
    static const int i = 0;
};

template<>
struct A<1> {
    static const int i = 1;
};

template <int N> const int A<N>::i;

int foo(int)
{
}

int bar(const int &)
{
}

int main()
{
    foo(A<1>::i); //ok here
    bar(A<0>::i); //g++ complains undefined reference to `A<0>::i'

    return 0;
}


-- 


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


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

* [Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
  2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
                   ` (8 preceding siblings ...)
  2009-12-08 14:06 ` aijunbai at gmail dot com
@ 2009-12-08 14:15 ` paolo dot carlini at oracle dot com
  9 siblings, 0 replies; 11+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-12-08 14:15 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from paolo dot carlini at oracle dot com  2009-12-08 14:15 -------
Since you have specializations for A, you also need, in general, the
corresponding definitions:

const int A<0>::i;
const int A<1>::i;


-- 


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


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

end of thread, other threads:[~2009-12-08 14:15 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-08  4:31 [Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function aijunbai at gmail dot com
2009-12-08  9:18 ` [Bug c++/42330] " paolo dot carlini at oracle dot com
2009-12-08  9:47 ` aijunbai at gmail dot com
2009-12-08  9:49 ` redi at gcc dot gnu dot org
2009-12-08  9:50 ` redi at gcc dot gnu dot org
2009-12-08 11:52 ` aijunbai at gmail dot com
2009-12-08 12:23 ` redi at gcc dot gnu dot org
2009-12-08 13:02 ` aijunbai at gmail dot com
2009-12-08 13:13 ` paolo dot carlini at oracle dot com
2009-12-08 14:06 ` aijunbai at gmail dot com
2009-12-08 14:15 ` paolo dot 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).