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

            Bug ID: 99066
           Summary: non-weak definition emitted for explicit instantiation
                    declaration
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: link-failure
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

If two TUs both include a header that contains:

template <typename a> struct basic_string {
  static const int npos = 1;
};
template <typename a> const int basic_string<a>::npos;

struct e { template <bool> int f() const; };

template <bool> int e::f() const {
  return basic_string<char>::npos;
}

extern template class basic_string<char>;

then we get a linker error, because both TUs define the npos constant:

/usr/bin/ld: /tmp/ccjy3Spq.o:(.rodata+0x0): multiple definition of
`basic_string<char>::npos'; /tmp/ccMQ33nr.o:(.rodata+0x0): first defined here


Shouldn't the static data member only have weak definitions, unless an explicit
instantiation definition is seen?

/* TU 1 */

#include "str.h"

int main()
{
  return e().f<true>();
}

/* TU 2 */

#include "str.h"


The assembly for both files contains:

        .globl  _ZN12basic_stringIcE4nposE
        .section        .rodata
        .align 4
        .type   _ZN12basic_stringIcE4nposE, @object
        .size   _ZN12basic_stringIcE4nposE, 4
_ZN12basic_stringIcE4nposE:
        .long   1

Reply via email to