Here are 3 functions and a dummy main:

--begin--
int one() { return 1; }

static int two() { return 2; }

namespace
{
  int three() { return 3; }
}

int main()
{
  return one() + two() + three();
}
--end--

i was expecting that g++ handled two() and three() the same way, that
is to say generated object-code with internal linkage.
 
But g++ doesn't. three() has external linkage, just like one(). TCPPPL
says that an anonymous namespace is a namespace with a unique
name. Fine. Then why g++ does not optimize this ?  The previous
program returns 6. g++ outputs a definition for three(), but it is
obvious that two() and three() definitions are no longer needed
because they've been inlined. That's why g++ doesn't output a
definition for two(). In the end, i get a never-used definition of
three() ...

Thanks

>>> LC_ALL=C g++-3.4 -v
Reading specs from /usr/lib/gcc/powerpc-linux/3.4.2/specs
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f77,pascal,objc,ada --prefix=/usr
--libexecdir=/usr/lib --with-gxx-include-dir=/usr/include/c++/3.4
--enable-shared --with-system-zlib --enable-nls --without-included-gettext
--program-suffix=-3.4 --enable-__cxa_atexit --enable-libstdcxx-allocator=mt
--enable-clocale=gnu --enable-libstdcxx-debug --enable-java-gc=boehm
--enable-java-awt=gtk --disable-multilib --disable-multilib powerpc-linux
Thread model: posix
gcc version 3.4.2 (Debian 3.4.2-3)

-- 
           Summary: external linkage of functions declared in an anonymous
                    namespace
           Product: gcc
           Version: 3.4.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: TazForEver at dlfp dot org
                CC: gcc-bugs at gcc dot gnu dot org


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

Reply via email to