bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org, Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: [PROPOSED 2/2] assert-h: prefer to ‘verify’
Date: Wed, 14 Sep 2022 15:43:30 +0200	[thread overview]
Message-ID: <3452626.iIbC2pHGDl@nimes> (raw)
In-Reply-To: <20220913033218.1011299-3-eggert@cs.ucla.edu>

Paul Eggert wrote:
> Where it’s easy, prefer ‘static_assert’ to ‘verify’,

Compiling a current GNU gettext with MSVC 14, I get this compilation error:

libtool: compile:  /home/bruno/msvc/compile cl -nologo -DIN_LIBASPRINTF -DHAVE_CONFIG_H -D__USE_MINGW_ANSI_STDIO=0 -I. -I../../../gettext-runtime/libasprintf -Ignulib-lib -I../../../gettext-runtime/libasprintf/gnulib-lib -D_WIN32_WINNT=_WIN32_WINNT_WIN7 -I/usr/local/msvc64/include -MD -c ../../../gettext-runtime/libasprintf/autosprintf.cc -o autosprintf.obj
autosprintf.cc
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xstddef(376): error C2720: 'std::hash<_Kty>::_gl_static_assert_function3': 'extern' storage-class specifier illegal on members
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xstddef(378): note: see reference to class template instantiation 'std::hash<_Kty>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(744): error C2720: 'std::_Change_sign<_Ty>::_gl_static_assert_function4': 'extern' storage-class specifier illegal on members
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(777): note: see reference to class template instantiation 'std::_Change_sign<_Ty>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(1193): error C2720: 'std::integer_sequence<_Ty,_Vals...>::_gl_static_assert_function5': 'extern' storage-class specifier illegal on members
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(1203): note: see reference to class template instantiation 'std::integer_sequence<_Ty,_Vals...>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(1502): error C2720: 'std::result_of<_Fty>::_gl_static_assert_function7': 'extern' storage-class specifier illegal on members
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(1505): note: see reference to class template instantiation 'std::result_of<_Fty>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(1580): error C2720: 'std::reference_wrapper<_Ty>::_gl_static_assert_function8': 'extern' storage-class specifier illegal on members
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(1612): note: see reference to class template instantiation 'std::reference_wrapper<_Ty>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(423): error C2720: 'std::tuple_element<_Idx,std::array<_Ty,_Size>>::_gl_static_assert_function9': 'extern' storage-class specifier illegal on members
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(426): note: see reference to class template instantiation 'std::tuple_element<_Idx,std::array<_Ty,_Size>>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(445): error C2059: syntax error: ')'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(447): note: see reference to class template instantiation 'std::tuple_element<_Index,std::tuple<>>' being compiled
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(445): error C2947: expecting '>' to terminate template-argument-list, found '>'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(445): error C2976: 'std::integral_constant': too few template arguments
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xtr1common(23): note: see declaration of 'std::integral_constant'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(447): error C2143: syntax error: missing ';' before '}'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\utility(447): fatal error C1004: unexpected end-of-file found
make[4]: *** [Makefile:1412: autosprintf.lo] Error 1

The situation with this compiler (in C++ mode) is:
  - It supports 'static_assert' with 2 arguments.
  - It does not support 'static_assert' with 1 argument.
  - Its header files make use of the 'static_assert' with 2 arguments,
    for example:

	template<size_t _Index>
	struct tuple_element<_Index, tuple<> >
	{	// enforce bounds checking
	static_assert(_Always_false<integral_constant<size_t, _Index> >::value,
		"tuple index out of bounds");
	};

Thus, our existing definition of static_assert via _Static_assert an _GL_VERIFY
does not work, because 'extern' is not allowed inside a struct/class definition.

This attempted patch

diff --git a/lib/verify.h b/lib/verify.h
index a0d597f3d4..608c9bda20 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -229,7 +229,13 @@ template <int w>
 # if (!defined static_assert \
       && __STDC_VERSION__ < 202311 \
       && __cpp_static_assert < 201411 && __GNUG__ < 6)
-#  define static_assert _Static_assert /* C11 requires this #define.  */
+#  if defined __cplusplus && _MSC_VER >= 1900
+/* MSVC 14 in C++ mode supports the 2-arguments static_assert but not the
+   one-argument static_assert, and it does not support _Static_assert.  */
+#   define static_assert(R, ...) static_assert((R), "static assertion failed")
+#  else
+#   define static_assert _Static_assert /* C11 requires this #define.  */
+#  endif
 # endif
 #endif
 
does not work either, because when we define static_assert as a macro, it
parses

static_assert(_Always_false<integral_constant<size_t, _Index> >::value,
		"tuple index out of bounds");

as a macro call with 3 arguments:
  argument 1:   _Always_false<integral_constant<size_t
  argument 2:   _Index> >::value
  argument 3:   "tuple index out of bounds"

Any ideas how to get this working?

Bruno





  parent reply	other threads:[~2022-09-14 13:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-13  3:32 [PROPOSED 0/2] static_assert and C23 Paul Eggert
2022-09-13  3:32 ` [PROPOSED 1/2] assert-h: static_assert is a keyword in C23 Paul Eggert
2022-09-13  3:32 ` [PROPOSED 2/2] assert-h: prefer to ‘verify’ Paul Eggert
2022-09-14  1:05   ` Bruno Haible
2022-09-14  4:47     ` Paul Eggert
2022-09-14  6:04       ` Bruno Haible
2022-09-14 11:35   ` Bruno Haible
2022-12-28 22:41     ` Paul Eggert
2022-09-14 13:43   ` Bruno Haible [this message]
2022-09-14 16:10     ` Bruno Haible
2022-09-14 20:22   ` Bruno Haible
2022-09-13 22:50 ` [PROPOSED 0/2] static_assert and C23 Bruno Haible
2022-09-13 23:06   ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3452626.iIbC2pHGDl@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).