Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb7f64c6

Browse files
committed
Refactor compile-time assertion checks in c.h
This commit refactors and simplifies the definitions of StaticAssertStmt,StaticAssertExpr and StaticAssertDecl. By unifying the C and C++fallback implementations, this reduces the number of differentimplementations from four to three.Author: Michael PaquierReviewed-by: Georgios Kokolatos, Tom LaneDiscussion:https://postgr.es/m/20200204081503.GF2287@paquier.xyz
1 parenta029a06 commitb7f64c6

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

‎src/include/c.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -836,43 +836,37 @@ extern void ExceptionalCondition(const char *conditionName,
836836
* The macro StaticAssertDecl() is suitable for use at file scope (outside of
837837
* any function).
838838
*
839+
* On recent C++ compilers, we can use standard static_assert().
840+
*
839841
* Otherwise we fall back on a kluge that assumes the compiler will complain
840842
* about a negative width for a struct bit-field. This will not include a
841843
* helpful error message, but it beats not getting an error at all.
842844
*/
843-
#ifndef__cplusplus
844-
#ifdefHAVE__STATIC_ASSERT
845+
#if !defined(__cplusplus)&& defined(HAVE__STATIC_ASSERT)
846+
/* Default C implementation */
845847
#defineStaticAssertStmt(condition,errmessage) \
846848
do { _Static_assert(condition, errmessage); } while(0)
847849
#defineStaticAssertExpr(condition,errmessage) \
848850
((void) ({ StaticAssertStmt(condition, errmessage); true; }))
849851
#defineStaticAssertDecl(condition,errmessage) \
850852
_Static_assert(condition, errmessage)
851-
#else/* !HAVE__STATIC_ASSERT */
852-
#defineStaticAssertStmt(condition,errmessage) \
853-
((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
854-
#defineStaticAssertExpr(condition,errmessage) \
855-
StaticAssertStmt(condition, errmessage)
856-
#defineStaticAssertDecl(condition,errmessage) \
857-
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
858-
#endif/* HAVE__STATIC_ASSERT */
859-
#else/* C++ */
860-
#if defined(__cpp_static_assert)&&__cpp_static_assert >=200410
853+
#elif defined(__cplusplus)&&__cpp_static_assert >=200410
854+
/* Default C++ implementation */
861855
#defineStaticAssertStmt(condition,errmessage) \
862856
static_assert(condition, errmessage)
863857
#defineStaticAssertExpr(condition,errmessage) \
864858
({ static_assert(condition, errmessage); })
865859
#defineStaticAssertDecl(condition,errmessage) \
866860
static_assert(condition, errmessage)
867-
#else/* !__cpp_static_assert */
861+
#else
862+
/* Fallback implementation for C and C++ */
868863
#defineStaticAssertStmt(condition,errmessage) \
869-
do {structstatic_assert_struct{ int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
864+
((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
870865
#defineStaticAssertExpr(condition,errmessage) \
871-
((void) ({StaticAssertStmt(condition, errmessage); }))
866+
StaticAssertStmt(condition, errmessage)
872867
#defineStaticAssertDecl(condition,errmessage) \
873868
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
874-
#endif/* __cpp_static_assert */
875-
#endif/* C++ */
869+
#endif
876870

877871

878872
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp