[PATCH] tst-unique3.cc: Explicitly instantiate S<char>::i

H.J. Lu hjl.tools@gmail.com
Tue Dec 17 17:34:09 GMT 2024


On Tue, Dec 17, 2024 at 11:10 PM Sam James <sam@gentoo.org> wrote:
>
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
> > Explicitly instantiate S<char>::i to silence Clang error:
> >
> > tst-unique3.cc:6:18: error: instantiation of variable 'S<char>::i' required here, but no definition is available [-Werror,-Wundefined-var-template]
> >     6 | int t = S<char>::i;
> >       |                  ^
> > ./tst-unique3.h:5:14: note: forward declaration of template entity is here
> >     5 |   static int i;
> >       |              ^
> > tst-unique3.cc:6:18: note: add an explicit instantiation declaration to suppress this warning if 'S<char>::i' is explicitly instantiated in another translation unit
> >     6 | int t = S<char>::i;
> >       |                  ^
> >
> > Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> > ---
>
> OK but try my trick from the other patch too, better if that works.

It doesn't work:

tst-unique3lib.cc:3:32: error: redefinition of 'i'
    3 | template<typename T> int S<T>::i = 1;
      |                                ^
./tst-unique3.h:5:21: note: previous definition is here
    5 |   inline static int i;
      |                     ^
1 error generated.

> >  elf/tst-unique3.cc | 3 +++
> >  1 file changed, 3 insertions(+)
> >
> > diff --git a/elf/tst-unique3.cc b/elf/tst-unique3.cc
> > index efdd6d78c2..bce3e578d7 100644
> > --- a/elf/tst-unique3.cc
> > +++ b/elf/tst-unique3.cc
> > @@ -3,6 +3,9 @@
> >  #include <cstdio>
> >  #include "../dlfcn/dlfcn.h"
> >
> > +template<>
> > +int S<char>::i;
> > +
> >  int t = S<char>::i;
> >
> >  int



-- 
H.J.


More information about the Libc-alpha mailing list