[Bug c++/42330] New: undefined reference to "static const int" in class when passing as "const int &" to a function
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
[Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
--- 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 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::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
[Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
--- 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::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 const A::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
[Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
--- Comment #7 from aijunbai at gmail dot com 2009-12-08 13:02 --- (In reply to comment #6) > (In reply to comment #5) > > > > > > template const A::i; > > > > > > > I tried so, but it seems do not work, could you please explain more > > detailedly? > > thx~ > > I missed the "int" out: > > template const int A::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
[Bug c++/42330] undefined reference to "static const int" in class when passing as "const int &" to a function
--- 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 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 const int A::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