Why does assert have to be implemented as an expression?

Discussion in 'C Programming' started by Jon, Oct 14, 2010.

  1. Jon

    Jon Guest

    Does it really matter, considering that it is a void expression, that
    assert is an expression instead of a statement?
     
    Jon, Oct 14, 2010
    #1
    1. Advertising

  2. Jon

    Ben Pfaff Guest

    "Jon" <> writes:

    > Does it really matter, considering that it is a void expression, that
    > assert is an expression instead of a statement?


    Sure. A void expression can be incorporated into a larger
    expression (e.g. with the comma operator). A statement cannot.
    --
    "Structure padding is the use of extraneous materials to
    enhance the shape of a struct and make it more attractive to
    members of the opposite struct. (See also "struct silicone.")"
    --Eric Sosman
     
    Ben Pfaff, Oct 14, 2010
    #2
    1. Advertising

  3. Jon

    Seebs Guest

    On 2010-10-14, Jon <> wrote:
    > Does it really matter, considering that it is a void expression, that
    > assert is an expression instead of a statement?


    Probably, because you can use void expressions in at least a few contexts
    in C:

    x ? (assert(y), 1) : ...

    More generally, the general rule in C is that things you could possibly
    implement as expressions tend to be expressions. (C doesn't go as far in
    this as, say, Ruby or Lua, but it's still pretty aggressive about it.)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Oct 14, 2010
    #3
  4. Jon

    Jon Guest

    Ben Pfaff wrote:
    > "Jon" <> writes:
    >
    >> Does it really matter, considering that it is a void expression, that
    >> assert is an expression instead of a statement?

    >
    > Sure. A void expression can be incorporated into a larger
    > expression (e.g. with the comma operator). A statement cannot.


    I knew that. Do you have an example? I recently changed my assert macro
    to be an expression and it DID clean things up nicely, but I think I
    don't know the whole gist of the requirement and whether it's just a
    corner case thing or a wildly-useful thing.
     
    Jon, Oct 14, 2010
    #4
  5. Jon

    Jon Guest

    Seebs wrote:
    > On 2010-10-14, Jon <> wrote:
    >> Does it really matter, considering that it is a void expression, that
    >> assert is an expression instead of a statement?

    >
    > Probably, because you can use void expressions in at least a few
    > contexts in C:
    >
    > x ? (assert(y), 1) : ...


    Would you write code like that? I wouldn't. Have a better example?

    >
    > More generally, the general rule in C is that things you could
    > possibly implement as expressions tend to be expressions.


    And I'd go where to study that issue? Wikipedia? I do find it relevant
    info, but, shouldn't it/that be knowledge?

    > (C doesn't
    > go as far in this as, say, Ruby or Lua, but it's still pretty
    > aggressive about it.)


    Your stage. I'll review the draft paper/article if you want (as I will
    ask many "dumb" questions).
     
    Jon, Oct 14, 2010
    #5
  6. Jon

    Seebs Guest

    On 2010-10-14, Jon <> wrote:
    > Would you write code like that?


    No.

    > I wouldn't. Have a better example?


    Not really, but I don't need one -- it's that way because people occasionally
    use assert() in larger expressions, possibly because an assertion is only
    relevant in some contexts.

    > And I'd go where to study that issue? Wikipedia? I do find it relevant
    > info, but, shouldn't it/that be knowledge?


    Read a bunch of C code and think about the "Spirit of C", pretty much;
    I guess you could read the Rationale for the standard.

    > Your stage. I'll review the draft paper/article if you want (as I will
    > ask many "dumb" questions).


    If you'll pay me for my time, I care what you think I should write. :)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Oct 14, 2010
    #6
  7. Jon

    Jon Guest

    Seebs wrote:
    > On 2010-10-14, Jon <> wrote:
    >> Would you write code like that?

    >
    > No.
    >
    >> I wouldn't. Have a better example?

    >
    > Not really, but I don't need one -- it's that way because people
    > occasionally use assert() in larger expressions, possibly because an
    > assertion is only relevant in some contexts.
    >
    >> And I'd go where to study that issue? Wikipedia? I do find it
    >> relevant info, but, shouldn't it/that be knowledge?

    >
    > Read a bunch of C code and think about the "Spirit of C", pretty much;
    > I guess you could read the Rationale for the standard.
    >
    >> Your stage. I'll review the draft paper/article if you want (as I
    >> will ask many "dumb" questions).

    >
    > If you'll pay me for my time, I care what you think I should write.
    > :)


    You KNOW I won't consult to you after that tantrum.
     
    Jon, Oct 14, 2010
    #7
  8. On 2010-10-14 07:42, Seebs wrote:
    > On 2010-10-14, Jon<> wrote:
    >> Does it really matter, considering that it is a void expression, that
    >> assert is an expression instead of a statement?

    >
    > Probably, because you can use void expressions in at least a few contexts
    > in C:
    >
    > x ? (assert(y), 1) : ...


    Oh no! Reminds me of a previous thread.


    /August

    --
    The competent programmer is fully aware of the limited size of his own
    skull. He therefore approaches his task with full humility, and avoids
    clever tricks like the plague. --Edsger Dijkstra
     
    August Karlstrom, Oct 14, 2010
    #8
  9. Jon

    Ben Pfaff Guest

    "Jon" <> writes:

    > Ben Pfaff wrote:
    >> "Jon" <> writes:
    >>
    >>> Does it really matter, considering that it is a void expression, that
    >>> assert is an expression instead of a statement?

    >>
    >> Sure. A void expression can be incorporated into a larger
    >> expression (e.g. with the comma operator). A statement cannot.

    >
    > I knew that. Do you have an example? I recently changed my assert macro
    > to be an expression and it DID clean things up nicely, but I think I
    > don't know the whole gist of the requirement and whether it's just a
    > corner case thing or a wildly-useful thing.


    I've never put an assertion into a larger expression in practice.
    --
    "Some people *are* arrogant, and others read the FAQ."
    --Chris Dollin
     
    Ben Pfaff, Oct 14, 2010
    #9
  10. (Ben Pfaff) writes:
    > "Jon" <> writes:
    >> Ben Pfaff wrote:
    >>> "Jon" <> writes:
    >>>
    >>>> Does it really matter, considering that it is a void expression, that
    >>>> assert is an expression instead of a statement?
    >>>
    >>> Sure. A void expression can be incorporated into a larger
    >>> expression (e.g. with the comma operator). A statement cannot.

    >>
    >> I knew that. Do you have an example? I recently changed my assert macro
    >> to be an expression and it DID clean things up nicely, but I think I
    >> don't know the whole gist of the requirement and whether it's just a
    >> corner case thing or a wildly-useful thing.

    >
    > I've never put an assertion into a larger expression in practice.


    But the fact that it can be used in an expression hasn't done you
    any harm, has it? (I know you didn't say it had.)

    assert() could have been written so it can only be used in a
    statement context, or so it can be used in an expression context
    (which of course means it can also be used as a statement by adding
    a semicolon). It wasn't significantly more difficult to provide the
    extra flexibility. If it could be used only in a statement context,
    it wouldn't have caused any great harm, but neither would there be
    any particular benefit in restricting it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 14, 2010
    #10
  11. Peter Nilsson <> writes:
    > "Jon" <> wrote:
    >> Does it really matter, considering that it is a void
    >> expression, that assert is an expression instead of a
    >> statement?

    >
    > Which kind of statement did you have in mind?


    Presumably an expression statement, consisting of an expression followed
    by a semicolon, such as:

    assert(x == 42);

    > And what do
    > you gain by making it a statement?


    Convenience. (A related question would be what you would gain by
    defining assert() so that it's only usable in a statement context;
    the answer, as far as I can tell, is nothing.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 15, 2010
    #11
  12. Keith Thompson <> writes:
    > Peter Nilsson <> writes:
    >> "Jon" <> wrote:
    >>> Does it really matter, considering that it is a void
    >>> expression, that assert is an expression instead of a
    >>> statement?

    >>
    >> Which kind of statement did you have in mind?

    >
    > Presumably an expression statement, consisting of an expression followed
    > by a semicolon, such as:
    >
    > assert(x == 42);
    >
    >> And what do
    >> you gain by making it a statement?

    >
    > Convenience. (A related question would be what you would gain by
    > defining assert() so that it's only usable in a statement context;
    > the answer, as far as I can tell, is nothing.)


    Sorry, I misunderstood the context. On re-reading, what I wrote makes
    sense, but not as a response to what Peter wrote.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 15, 2010
    #12
  13. Keith Thompson <> wrote:
    > Peter Nilsson <> writes:
    > > "Jon" <> wrote:
    > > > Does it really matter, considering that it is a void
    > > > expression, that assert is an expression instead of a
    > > > statement?

    > >
    > > Which kind of statement did you have in mind?

    >
    > Presumably an expression statement, consisting of an
    > expression followed by a semicolon, such as:
    >
    > assert(x == 42);


    I was imagining the OP wanted something like...

    assert-statement:
    assert expression ;

    e.g.

    assert x == 42;

    > > And what do you gain by making it a statement?

    >
    > Convenience.


    What convenience?

    > (A related question would be what you would gain by
    > defining assert() so that it's only usable in a
    > statement context; the answer, as far as I can tell,
    > is nothing.)


    Even if it was defined that way, it's hard to imagine
    why anyone would go to the trouble of implementing a
    macro that enforces the rule!

    --
    Peter
     
    Peter Nilsson, Oct 15, 2010
    #13
  14. Jon

    Eric Sosman Guest

    On 10/14/2010 12:58 PM, Ben Pfaff wrote:
    > "Jon"<> writes:
    >
    >> Ben Pfaff wrote:
    >>> "Jon"<> writes:
    >>>
    >>>> Does it really matter, considering that it is a void expression, that
    >>>> assert is an expression instead of a statement?
    >>>
    >>> Sure. A void expression can be incorporated into a larger
    >>> expression (e.g. with the comma operator). A statement cannot.

    >>
    >> I knew that. Do you have an example? I recently changed my assert macro
    >> to be an expression and it DID clean things up nicely, but I think I
    >> don't know the whole gist of the requirement and whether it's just a
    >> corner case thing or a wildly-useful thing.

    >
    > I've never put an assertion into a larger expression in practice.


    I have, but only in the depths of debugging:

    for (i = 0; assert(i < N), haystack != needle; ++i)
    ;

    Can't recall letting such a thing survive very long, though. If
    the assert() was worth keeping, I'd rearrange the loop to give it
    a separate existence, e.g.:

    for (i = 0; ; ++i) {
    assert(i < N);
    if (haystack == needle)
    break;
    }

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 15, 2010
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Brewer
    Replies:
    1
    Views:
    491
    bsmith
    Nov 7, 2004
  2. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,526
    Carl Banks
    Feb 23, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,003
    Smokey Grindel
    Dec 2, 2006
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    918
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,671
    Richard Bos
    May 17, 2010
Loading...

Share This Page