* BUILD_BUG_ON_ZERO
@ 2011-02-25 10:50 Zhang Meng
2011-02-25 16:33 ` BUILD_BUG_ON_ZERO Dave Hylands
0 siblings, 1 reply; 3+ messages in thread
From: Zhang Meng @ 2011-02-25 10:50 UTC (permalink / raw)
To: kernelnewbies
Hi ~List,
Could anybody explain the macro below? what does it mean?
Thanks in advance.
#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
--
Yours sincerely
ZhangMeng
^ permalink raw reply [flat|nested] 3+ messages in thread
* BUILD_BUG_ON_ZERO
2011-02-25 10:50 BUILD_BUG_ON_ZERO Zhang Meng
@ 2011-02-25 16:33 ` Dave Hylands
2011-02-26 1:14 ` BUILD_BUG_ON_ZERO Meng Zhang
0 siblings, 1 reply; 3+ messages in thread
From: Dave Hylands @ 2011-02-25 16:33 UTC (permalink / raw)
To: kernelnewbies
Hi Zhang,
On Fri, Feb 25, 2011 at 3:50 AM, Zhang Meng <jammy.linux@gmail.com> wrote:
> Hi ~List,
>
> Could anybody explain the macro below? what does it mean?
>
> Thanks in advance.
>
> #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
This is also known as a compile time assert. I think that this
particular variant has to be used inside a function.
! is just negation and produces a zero or 1 result. !! just does it
twice, so that a non-zero value coming in becomes 1, and a zero value
remains as zero.
If e evaluates to false (zero) then -!!(e) evaluates to zero;
if e evaluates to true (non-zero) then -!!(e) evaluates to -1.
Declaring a bit field with a size of -1 will cause a compiler error.
I'm actually surprised that declaring a bitfield of size 0 works.
The typical declarations of this I've seen usually use arrays and
arrange for the size to be -1 or 1 (which is generally more portable).
When you use the array style declaration, you can use it outside a
function as well.
Dave Hylands
^ permalink raw reply [flat|nested] 3+ messages in thread
* BUILD_BUG_ON_ZERO
2011-02-25 16:33 ` BUILD_BUG_ON_ZERO Dave Hylands
@ 2011-02-26 1:14 ` Meng Zhang
0 siblings, 0 replies; 3+ messages in thread
From: Meng Zhang @ 2011-02-26 1:14 UTC (permalink / raw)
To: kernelnewbies
On Sat, Feb 26, 2011 at 12:33 AM, Dave Hylands <dhylands@gmail.com> wrote:
> Hi Zhang,
>
> On Fri, Feb 25, 2011 at 3:50 AM, Zhang Meng <jammy.linux@gmail.com> wrote:
>> Hi ~List,
>>
>> Could anybody explain the macro below? what does it mean?
>>
>> Thanks in advance.
>>
>> #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
>
> This is also known as a compile time assert. I think that this
> particular variant has to be used inside a function.
>
> ! is just negation and produces a zero or 1 result. !! just does it
> twice, so that a non-zero value coming in becomes 1, and a zero value
> remains as zero.
>
> If e evaluates to false (zero) then -!!(e) evaluates to zero;
> if e evaluates to true (non-zero) then -!!(e) evaluates to -1.
>
> Declaring a bit field with a size of -1 will cause a compiler error.
> I'm actually surprised that declaring a bitfield of size 0 works.
>
> The typical declarations of this I've seen usually use arrays and
> arrange for the size to be -1 or 1 (which is generally more portable).
> When you use the array style declaration, you can use it outside a
> function as well.
>
> Dave Hylands
>
Thanks Dave.
My dilemma also comes from declaring a anonymous bitfield of size 0.
The following one is easy to understand anyway.
#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
--
Yours sincerely
ZhangMeng
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-02-26 1:14 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-25 10:50 BUILD_BUG_ON_ZERO Zhang Meng
2011-02-25 16:33 ` BUILD_BUG_ON_ZERO Dave Hylands
2011-02-26 1:14 ` BUILD_BUG_ON_ZERO Meng Zhang
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 NNTP newsgroup(s).