Bug #9527

Feature #6951: Initial c11 support

assert.h: static_assert macro should be guarded by !defined(__cplusplus)

Added by Andy Fiddaman 10 months ago. Updated 9 months ago.

Status:ClosedStart date:2018-05-09
Priority:NormalDue date:
Assignee:Andy Fiddaman% Done:

100%

Category:-
Target version:-
Difficulty:Bite-size Tags:

Description

In /usr/include/assert.h, static_assert() is redefined to _Static_assert() if using C11 or higher.

/*
 * In C11 the static_assert macro is always defined, unlike the assert macro.
 */
#if __STDC_VERSION__ - 0 >= 201112L
#define static_assert   _Static_assert
#endif /* __STDC_VERSION - 0 >= 201112L */

This should only be done for plain C, not C++.. cf. other operating systems such as FreeBSD - http://src.illumos.org/source/xref/freebsd-head/include/assert.h#72

Using the following test program:

#include <cassert>
#include <cstdlib>
#include <type_traits>

int main() {}

It fails to compile with:

/opt/gcc-7/include/c++/7.3.0/type_traits:2079:21: error: expected identifier before 'sizeof'
       static_assert(sizeof...(_Types) != 0, "At least one type is required");
...

With the following patch, compilation is successful:

--- a/usr/src/head/assert.h
+++ b/usr/src/head/assert.h
@@ -46,9 +46,9 @@ extern void __assert(const char *, const char *, int);
 /*
  * In C11 the static_assert macro is always defined, unlike the assert macro.
  */
-#if __STDC_VERSION__ - 0 >= 201112L
+#if __STDC_VERSION__ - 0 >= 201112L && !defined(__cplusplus)
 #define        static_assert   _Static_assert
-#endif /* __STDC_VERSION - 0 >= 201112L */
+#endif /* __STDC_VERSION - 0 >= 201112L && !defined(__cplusplus) */

 #ifdef __cplusplus
 }

History

#1 Updated by Andy Fiddaman 10 months ago

Two more useful links from Yuri Pankov

http://en.cppreference.com/w/c/error/static_assert

shows the C form as

#define static_assert _Static_assert
static_assert(2 + 2 == 4, "2+2 isn't 4");

whereas http://en.cppreference.com/w/cpp/language/static_assert

shows the C++ form as just

   static_assert(std::is_copy_constructible<T>::value, "Swap requires copying");

So the macro should not be applied to C++.

#2 Updated by Andy Fiddaman 9 months ago

  • Tags deleted (needs-triage)
  • Status changed from New to Pending RTI

#3 Updated by Electric Monk 9 months ago

  • % Done changed from 0 to 100
  • Status changed from Pending RTI to Closed

git commit 8dc6d85a3768199d9f1acfd636fa366067f4aa18

commit  8dc6d85a3768199d9f1acfd636fa366067f4aa18
Author: Andy Fiddaman <omnios@citrus-it.co.uk>
Date:   2018-05-11T15:00:40.000Z

    9527 assert.h: static_assert macro should be guarded by !defined(__cplusplus)
    Reviewed by: Igor Kozhukhov <igor@dilos.org>
    Reviewed by: Toomas Soome <toomas@me.com>
    Reviewed by: Dominik Hassler <hadfl@omniosce.org>
    Reviewed by: Robert Mustacchi <robert.mustacchi@joyent.com>
    Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
    Approved by: Dan McDonald <danmcd@joyent.com>

Also available in: Atom