]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
stdcountof-h: Add support for string literal arguments in C++ mode.
authorBruno Haible <bruno@clisp.org>
Wed, 8 Oct 2025 12:10:13 +0000 (14:10 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 8 Oct 2025 12:13:20 +0000 (14:13 +0200)
Reported by Alejandro Colomar <alx@kernel.org> in
<https://lists.gnu.org/archive/html/bug-gnulib/2025-10/msg00010.html>.

* lib/stdcountof.in.h (_gl_array_type_test): Add a partial instantiation
for string literals.

ChangeLog
lib/stdcountof.in.h

index b8f98a18ae8c363590630b4d7309fbdf531f6fbe..2fa327c5d5df25bb64b0e625ab0f7eba38d2c112 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2025-10-08  Bruno Haible  <bruno@clisp.org>
+
+       stdcountof-h: Add support for string literal arguments in C++ mode.
+       Reported by Alejandro Colomar <alx@kernel.org> in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2025-10/msg00010.html>.
+       * lib/stdcountof.in.h (_gl_array_type_test): Add a partial instantiation
+       for string literals.
+
 2025-10-03  Bruno Haible  <bruno@clisp.org>
 
        localename-unsafe: Fix handling of yue-Hans locale on macOS.
index 93c7f6bb75d84ec39354b13595dd2d2e226088e2..6e602b4a9ffccbd29267421a2ed0b7c3326cb17b 100644 (file)
@@ -72,11 +72,15 @@ template <typename T>
 /* Bounded arrays.  */
 template <typename T, size_t N>
   struct _gl_array_type_test<T[N]> { static const int is_array = 1; };
+/* String literals.  */
+template <typename T, size_t N>
+  struct _gl_array_type_test<T const (&)[N]> { static const int is_array = 1; };
 #   define _gl_verify_is_array(a) \
      sizeof (_gl_verify_type<_gl_array_type_test<decltype(a)>::is_array>)
 #  else
   /* Use template argument deduction.
-     Use sizeof to get a constant expression from an unknown type.  */
+     Use sizeof to get a constant expression from an unknown type.
+     Note: This approach does not work for countof (((int[]) { a, b, c })).  */
 /* Default case.  */
 template <typename T>
   struct _gl_array_type_test { double large; };