kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: dhylands@gmail.com (Dave Hylands)
To: kernelnewbies@lists.kernelnewbies.org
Subject: BUILD_BUG_ON_ZERO
Date: Fri, 25 Feb 2011 09:33:53 -0700	[thread overview]
Message-ID: <AANLkTimNQgJjGzk8M7QkXAy-cwcHFPA54rqjAgWFPzjv@mail.gmail.com> (raw)
In-Reply-To: <AANLkTim3kugPOv9F4z5=G3x2P05HChgT2tK_KFsumPPf@mail.gmail.com>

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

  reply	other threads:[~2011-02-25 16:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-25 10:50 BUILD_BUG_ON_ZERO Zhang Meng
2011-02-25 16:33 ` Dave Hylands [this message]
2011-02-26  1:14   ` BUILD_BUG_ON_ZERO Meng Zhang

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AANLkTimNQgJjGzk8M7QkXAy-cwcHFPA54rqjAgWFPzjv@mail.gmail.com \
    --to=dhylands@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /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 NNTP newsgroup(s).