IMO, in the following test program the variable s within the class template may
be optimized-away in every case. Although, the variable is used as an argument
for a function call, it is omit-able due to the definition of function t1 as
"static inline" with its empty body. If I use instead of a class template a
function template, this contained static local variable will be optimized-away,
as expected.

[mschu...@teeth tst]$ cat test.cc
static inline void t1(const char* s) {}

template <typename T>
struct class_template {
    class_template() {
        static const char s[]="class_template";
        t1(s);
    }
};

template <typename T>
static inline void function_template() {
        static const char s[]="function_template";
        t1(s);
    }

int main(int, char**) {
    class_template<int> t;
    function_template<int>();
    return 0;
}

I compiled the program with

[mschu...@teeth tst]$ g++ -v
Using built-in specs.
Target: i586-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions
--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.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib
--with-ppl --with-cloog --with-tune=generic --with-arch=i586
--build=i586-redhat-linux
Thread model: posix
gcc version 4.4.1 20090725 (Red Hat 4.4.1-2) (GCC)

and then I used the string program to list the contained strings.

[mschu...@teeth tst]$ strings t
/lib/ld-linux.so.2
libstdc++.so.6
__gmon_start__
_Jv_RegisterClasses
__gxx_personality_v0
libm.so.6
libgcc_s.so.1
libc.so.6
_IO_stdin_used
__libc_start_main
CXXABI_1.3
GLIBC_2.0
PTRhP
QVhD
[^_]
class_template
[mschu...@teeth tst]$

The class_template string is present, but the function_template string not. If
I omit the static keyword, it will disappear, too.



Thanks in advance,
Michael


-- 
           Summary: static local variables in class template methods are not
                    optimized-away if not used
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mschulze at ivs dot cs dot ovgu dot de
 GCC build triplet: i586-redhat-linux
  GCC host triplet: i586-redhat-linux
GCC target triplet: i586-redhat-linux


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

Reply via email to