https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61882

            Bug ID: 61882
           Summary: attribute weak ignored for function templates
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gmail dot com

In GCC 4.5 and later, and at -O and above, attribute weak (but not weak alias)
is silently ignored on declarations of function templates and calls to
specializations of such templates are inlined into their callers.

The program below shows that GCC inlines calls to the function template
specialization instantiated from the weak primary template baz, but emits a
weak symbol for the weak alias foo<int>. GCC 4.2 honored the attribute and
emitted a weak symbol for both foo<int> and baz<int>. Clang 3.4 also emits a
weak symbol for both as expected. Disabling optimization changes the current
behavior to match that of GCC 4.2.1 and Clang 3.4.

The GCC documentation of attribute weak doesn't say whether or not the
attribute is intended to have the same effect on specializations of function
templates as it does on ordinary functions. If the current GCC behavior is
intended, the documentation should be updated to make it clear when attribute
weak is ignored, and GCC should be enhanced to issue a warning when the
attribute is ignored.

$ (set -x; cc=/auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++; cat z.c &&
$cc -c -fpic -DFOO=1 -O -Wall -Wextra z.c && $cc -fpic -Wall -Wextra z.c z.o &&
./a.out)
+ cc=/auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++
+ cat z.c
template <class T> void foo (T);
template <class T> void baz (T);
void foobar ();

#if FOO
template <class T> void __attribute__ ((weak, alias ("bar"))) foo (T);

static void bar (int) { }

template <class T> void __attribute__ ((weak)) baz (T) { }

void foobar () { foo (0), baz (0); }

#else

#  include <stdio.h>

template <> void foo (int) { puts (__func__); }
template <> void baz (int) { puts (__func__); }

int main (void) { foobar (); }

#endif

+ /auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++ -c -fpic -DFOO=1 -O -Wall
-Wextra z.c
+ /auto/compiler-dev/msebor/contrib/cel-5.50/bin/g++ -fpic -Wall -Wextra z.c
z.o
+ ./a.out
foo<int>

Reply via email to