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 18:10:43 +0200	[thread overview]
Message-ID: <3122478.5fSG56mABF@nimes> (raw)
In-Reply-To: <3452626.iIbC2pHGDl@nimes>

> > Where it’s easy, prefer ‘static_assert’ to ‘verify’,
> 
> Compiling a current GNU gettext with MSVC 14, I get this compilation error:

I got this working, by using preprocessor tricks, inspired by
  <https://github.com/Hirrolot/awesome-c-preprocessor>
  <https://stackoverflow.com/questions/11761703/>
  <https://groups.google.com/g/comp.std.c/c/d-6Mj5Lko_s?pli=1>
  <https://stackoverflow.com/questions/5134523/>


2022-09-14  Bruno Haible  <bruno@clisp.org>

	verify: Avoid syntax error due to static_assert with MSVC 14 in C++.
	* lib/verify.h (static_assert): For MSVC in C++ mode, use a definition
	that supports both the two-arguments and the one-argument syntax.

diff --git a/lib/verify.h b/lib/verify.h
index a0d597f3d4..0f5f0edc86 100644
--- a/lib/verify.h
+++ b/lib/verify.h
@@ -229,7 +229,22 @@ 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 && !defined __clang__
+/* MSVC 14 in C++ mode supports the two-arguments static_assert but not
+   the one-argument static_assert, and it does not support _Static_assert.
+   We have to play preprocessor tricks to distinguish the two cases.
+   Since the MSVC preprocessor is not ISO C compliant (cf.
+   <https://stackoverflow.com/questions/5134523/>), the solution is specific
+   to MSVC.  */
+#   define _GL_EXPAND(x) x
+#   define _GL_SA1(a1) static_assert ((a1), "static assertion failed")
+#   define _GL_SA2 static_assert
+#   define _GL_SA3 static_assert
+#   define _GL_SA_PICK(x1,x2,x3,x4,...) x4
+#   define static_assert(...) _GL_EXPAND(_GL_SA_PICK(__VA_ARGS__,_GL_SA3,_GL_SA2,_GL_SA1)) (__VA_ARGS__)
+#  else
+#   define static_assert _Static_assert /* C11 requires this #define. */
+#  endif
 # endif
 #endif
 





  reply	other threads:[~2022-09-14 16:55 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
2022-09-14 16:10     ` Bruno Haible [this message]
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=3122478.5fSG56mABF@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).