Re: Variably modified at file scope due to (void *)0 == (sem_t *)0?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 04/10/2018 14:27, Liu Hao wrote:

The same expression in the
_Static_assert() doen't lead to a warning.
It does with -pedantic, and that makes the reason for the warning clearer:

foo.c:5:26: warning: expression in static assertion is not an integer
constant expression [-Wpedantic]
  _Static_assert((void *)0 == (sem_t *)0, "sem_failed");

This looks bad, so basically a pointer constant doesn't exist in standard C?


As a difference from C++, C does not allow pointer operands in an /integer constant expression/, despite the type of results of pointer comparison, which is `int`.

Because of VLAs, it is discouraged to use array extents for such purpose. You may prefer bitfield widths, which would result in a better message:

```
struct static_assert_sem_failed
  { int unused : ((void *)0 == (sem_t *)0) ? 1 : -1; };

/*
lh_mouse@lhmouse-ideapad ~/Desktop $ gcc test.5.c  -Wall -Wextra -Wpedantic -Werror test.5.c:4:9: error: bit-field ‘unused’ width not an integer constant expression [-Werror=pedantic]
   { int unused : ((void *)0 == (sem_t *)0) ? 1 : -1; };
*/
```

Thanks for highlighting this C/C++ difference with respect to pointer operands. In C++ it looks better.

#ifdef __cplusplus
#define _Static_assert static_assert
#endif

typedef struct { int i; } sem_t;

struct static_assert_sem_failed { int unused : ((void *)0 == (sem_t *)0) ? 1 : -1; };

_Static_assert((void *)0 == (sem_t *)0, "sem_failed");

void f(void)
{
  struct f_static_assert_sem_failed { int unused : ((void *)0 == (sem_t *)0) ? 1 : -1; };

  _Static_assert((void *)0 == (sem_t *)0, "sem_failed");
}

gcc -S -x c++ -o - sa.c > /dev/null -Wall -Wextra -pedantic

No warnings or errors.

gcc -S -x c -o - sa.c > /dev/null -Wall -Wextra -pedantic
sa.c:7:39: warning: bit-field ‘unused’ width not an integer constant expression [-Wpedantic]  struct static_assert_sem_failed { int unused : ((void *)0 == (sem_t *)0) ? 1 : -1; };
                                       ^~~~~~
sa.c:9:26: warning: expression in static assertion is not an integer constant expression [-Wpedantic]
 _Static_assert((void *)0 == (sem_t *)0, "sem_failed");
                ~~~~~~~~~~^~~~~~~~~~~~~
sa.c: In function ‘f’:
sa.c:13:43: warning: bit-field ‘unused’ width not an integer constant expression [-Wpedantic]    struct f_static_assert_sem_failed { int unused : ((void *)0 == (sem_t *)0) ? 1 : -1; };
                                           ^~~~~~
sa.c:15:28: warning: expression in static assertion is not an integer constant expression [-Wpedantic]
   _Static_assert((void *)0 == (sem_t *)0, "sem_failed");

Only a pedantic warning unlike to the variant with the array.

--
Sebastian Huber, embedded brains GmbH

Address : Dornierstr. 4, D-82178 Puchheim, Germany
Phone   : +49 89 189 47 41-16
Fax     : +49 89 189 47 41-09
E-Mail  : sebastian.huber@xxxxxxxxxxxxxxxxxx
PGP     : Public key available on request.

Diese Nachricht ist keine geschäftliche Mitteilung im Sinne des EHUG.





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux