[PATCH v2] tst-unique[34].cc: Use explicit instantiation declaration/definition

Sam James sam@gentoo.org
Thu Jan 2 18:42:06 GMT 2025


"H.J. Lu" <hjl.tools@gmail.com> writes:

> Use explicit instantiation declaration and definition 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;
>       |                  ^
>

Reviewed-by: Sam James <sam@gentoo.org>

> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  elf/tst-unique3.cc    |  3 +--
>  elf/tst-unique4.cc    | 31 +++++++++++++++++++++++++------
>  elf/tst-unique4lib.cc | 39 +++++++++++++++++++++++++--------------
>  3 files changed, 51 insertions(+), 22 deletions(-)
>
> diff --git a/elf/tst-unique3.cc b/elf/tst-unique3.cc
> index bce3e578d7..07a21ef4ad 100644
> --- a/elf/tst-unique3.cc
> +++ b/elf/tst-unique3.cc
> @@ -3,8 +3,7 @@
>  #include <cstdio>
>  #include "../dlfcn/dlfcn.h"
>  
> -template<>
> -int S<char>::i;
> +extern template struct S<char>;
>  
>  int t = S<char>::i;
>  
> diff --git a/elf/tst-unique4.cc b/elf/tst-unique4.cc
> index 46a42f15c2..7212367fa3 100644
> --- a/elf/tst-unique4.cc
> +++ b/elf/tst-unique4.cc
> @@ -2,12 +2,32 @@
>  #include "tst-unique4.h"
>  
>  #include <cstdio>
> -#include <libc-diag.h>
>  
> -/* clang warns that the instantiation of the variable is required, but no
> -   definition is available.  They are implemented on tst-unique4lib.so.  */
> -DIAG_PUSH_NEEDS_COMMENT_CLANG;
> -DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wundefined-var-template");
> +extern template struct S<1>;
> +extern template struct S<2>;
> +extern template struct S<3>;
> +extern template struct S<4>;
> +extern template struct S<5>;
> +extern template struct S<6>;
> +extern template struct S<7>;
> +extern template struct S<8>;
> +extern template struct S<9>;
> +extern template struct S<10>;
> +extern template struct S<11>;
> +extern template struct S<12>;
> +extern template struct S<13>;
> +extern template struct S<14>;
> +extern template struct S<15>;
> +extern template struct S<16>;
> +extern template struct S<17>;
> +extern template struct S<18>;
> +extern template struct S<19>;
> +extern template struct S<20>;
> +extern template struct S<21>;
> +extern template struct S<22>;
> +extern template struct S<23>;
> +extern template struct S<24>;
> +
>  static int a[24] =
>    {
>      S<1>::i, S<2>::i, S<3>::i, S<4>::i, S<5>::i, S<6>::i, S<7>::i, S<8>::i,
> @@ -15,7 +35,6 @@ static int a[24] =
>      S<16>::i, S<17>::i, S<18>::i, S<19>::i, S<20>::i, S<21>::i, S<22>::i,
>      S<23>::i, S<24>::i
>    };
> -DIAG_POP_NEEDS_COMMENT_CLANG;
>  
>  int
>  main (void)
> diff --git a/elf/tst-unique4lib.cc b/elf/tst-unique4lib.cc
> index 2829e75cee..7afbe72a0e 100644
> --- a/elf/tst-unique4lib.cc
> +++ b/elf/tst-unique4lib.cc
> @@ -1,17 +1,28 @@
>  // BZ 12511
>  #include "tst-unique4.h"
>  
> -template<int N>
> -int S<N>::i __attribute__ ((used)) = N;
> -template<int N>
> -const int S<N>::j __attribute__ ((used)) = -1;
> -
> -static int a[24] __attribute__ ((used)) =
> -  {
> -    S<1>::i, S<2>::i, S<3>::i, S<4>::i, S<5>::i, S<6>::i, S<7>::i, S<8>::i,
> -    S<9>::i, S<10>::i, S<11>::i, S<12>::i, S<13>::i, S<14>::i, S<15>::i,
> -    S<16>::i, S<17>::i, S<18>::i, S<19>::i, S<20>::i, S<21>::i, S<22>::i,
> -    S<23>::i, S<24>::i
> -  };
> -
> -static int b __attribute__ ((used)) = S<1>::j;
> +template<> int S<1>::i = 1;
> +template<> int S<2>::i = 2;
> +template<> int S<3>::i = 3;
> +template<> int S<4>::i = 4;
> +template<> int S<5>::i = 5;
> +template<> int S<6>::i = 6;
> +template<> int S<7>::i = 7;
> +template<> int S<8>::i = 8;
> +template<> int S<9>::i = 9;
> +template<> int S<10>::i = 10;
> +template<> int S<11>::i = 11;
> +template<> int S<12>::i = 12;
> +template<> int S<13>::i = 13;
> +template<> int S<14>::i = 14;
> +template<> int S<15>::i = 15;
> +template<> int S<16>::i = 16;
> +template<> int S<17>::i = 17;
> +template<> int S<18>::i = 18;
> +template<> int S<19>::i = 19;
> +template<> int S<20>::i = 20;
> +template<> int S<21>::i = 21;
> +template<> int S<22>::i = 22;
> +template<> int S<23>::i = 23;
> +template<> int S<24>::i = 24;
> +template<> const int S<1>::j = -1;


More information about the Libc-alpha mailing list