This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/33876] New: in-class initialization of const static data members and template functions


const static data members of an integral type, which are initialized in the
class declaration can not be used in templated functions if these functions
take references as arguments. The linker reports an undefined reference to that
member.

The problem occured e.g. with std::min() and std:max() from <algorithm>.
The code below reproduces the error.

---- snip -- bug.c: --
template <class T>
const T& min(const T &a, const T &b)
{
  return (a < b ? a : b);
}
template <class T>
inline const T& max(const T& a, const T& b)
{
  return (a > b ? a : b);
}
template <class T>
inline const T mux(const T a, const T b)
{
  return (a > b ? a : b);
}
class A
{
public:
  static const int x = 1;  // in-class init
  static const int y;
};
const int A::y = 1;  // out of class init
int main(int argc, char **argv)
{
  int i = 2;
  int j = min(i, A::x);  // undefined reference with and without -O
  int k = max(i, A::x);  // OK with -O, undefined reference without -O
  int l = mux(i, A::x);  // OK with and without -O
  int m = min(i, A::y);  // OK with and without -O
  int n = max(i, A::y);  // OK with and without -O
  int o = mux(i, A::y);  // OK with and without -O
}
--- snip ----

This is the output when compiling without and with optimization -O.
Note that the error goes away for the inlined template method when turning
optimization on.
Note also that the static const member which is initialized outside of the
class declaration never poses a problem.

---- snip ----
> g++ bug.c
/tmp/cc6W3wcw.o: In function `main':
bug.c:(.text+0x1c): undefined reference to `A::x'
bug.c:(.text+0x34): undefined reference to `A::x'
collect2: ld returned 1 exit status

> g++ bug.c -O
/tmp/ccCaNIHE.o: In function `main':
bug.c:(.text+0x21): undefined reference to `A::x'
collect2: ld returned 1 exit status
---- snip ----

This is the output of gcc -v

---- snip ----
> gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic
--host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20070626 (Red Hat 4.1.2-13)
---- snip ----

The machine is an Intel Core Duo T2600 running Fedora Core 6.


-- 
           Summary: in-class initialization of const static data members and
                    template functions
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: michael dot zillich at gmx dot net
 GCC build triplet: gcc version 4.1.2 20070626 (Red Hat 4.1.2-13)
GCC target triplet: i386-redhat-linux


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]