public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/61882] New: attribute weak ignored for function templates
@ 2014-07-22 16:40 msebor at gmail dot com
  2014-07-27 20:16 ` [Bug c++/61882] " olegendo at gcc dot gnu.org
  2023-03-07 15:43 ` pexu@gcc-bugzilla.mail.kapsi.fi
  0 siblings, 2 replies; 3+ messages in thread
From: msebor at gmail dot com @ 2014-07-22 16:40 UTC (permalink / raw)
  To: gcc-bugs

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>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-03-07 15:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-22 16:40 [Bug c++/61882] New: attribute weak ignored for function templates msebor at gmail dot com
2014-07-27 20:16 ` [Bug c++/61882] " olegendo at gcc dot gnu.org
2023-03-07 15:43 ` pexu@gcc-bugzilla.mail.kapsi.fi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).