Any use for the comma operator?

Discussion in 'C++' started by Rui Maciel, Aug 16, 2012.

  1. Rui Maciel

    Rui Maciel Guest

    Did anyone ever used the comma operator in some place other than a loop
    condition?


    Thanks in advance,
    Rui Maciel
    Rui Maciel, Aug 16, 2012
    #1
    1. Advertising

  2. On 8/16/2012 12:20 PM, Rui Maciel wrote:
    > Did anyone ever used the comma operator in some place other than a loop
    > condition?


    Absolutely. If you can find a reference on the 'net to "list assignment
    trick" or "technique", you can implement your own list class in such a
    way that would allow writing something like

    MyList<int> mylist;
    ...
    mylist = 1, 2, 4;

    for instance. I honestly have no opinion on convenience of such code.
    But it looks like fun.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 16, 2012
    #2
    1. Advertising

  3. Rui Maciel

    Kaba Guest

    16.8.2012 19:20, Rui Maciel kirjoitti:
    > Did anyone ever used the comma operator in some place other than a loop
    > condition?


    Consider a class A which is a container of some kind. Then with suitable
    definitions it could be filled with the syntax

    A a;
    a += 1, 2, 3, 4;

    I did that for my matrix class. However, with C++11 and initializer
    lists such uses should decline.

    --
    http://kaba.hilvi.org
    Kaba, Aug 16, 2012
    #3
  4. Rui Maciel

    Luca Risolia Guest

    On 16/08/2012 18:20, Rui Maciel wrote:
    > Did anyone ever used the comma operator in some place other than a loop
    > condition?


    Sometimes I used it to dynamically initialize some static data before
    main(), for example:

    // .h
    struct S {
    static void compute() { // non-const expr
    // ...
    }
    static int x[], y[];
    static bool init;
    };

    // .cpp
    int S::x[100], y[100];
    bool S::init = (S::compute(), true);
    Luca Risolia, Aug 16, 2012
    #4
  5. Rui Maciel

    Rui Maciel Guest

    Victor Bazarov wrote:

    > On 8/16/2012 12:20 PM, Rui Maciel wrote:
    >> Did anyone ever used the comma operator in some place other than a loop
    >> condition?

    >
    > Absolutely. If you can find a reference on the 'net to "list assignment
    > trick" or "technique", you can implement your own list class in such a
    > way that would allow writing something like
    >
    > MyList<int> mylist;
    > ...
    > mylist = 1, 2, 4;
    >
    > for instance. I honestly have no opinion on convenience of such code.
    > But it looks like fun.


    Thanks for mentioning that trick. Meanwhile, while googling for "list
    assignment trick" I've stumbled on Boost's assignment library, which might
    be of some interest:

    http://www.boost.org/doc/libs/1_47_0/libs/assign/doc/index.html


    With C++11's initializer lists this trick might go the way of the dodo, but
    it is still a neat trick, though.


    Rui Maciel
    Rui Maciel, Aug 16, 2012
    #5
  6. Rui Maciel

    Kaba Guest

    16.8.2012 22:21, Rui Maciel kirjoitti:
    > With C++11's initializer lists this trick might go the way of the dodo, but
    > it is still a neat trick, though.


    Well, yes:) More generally, though, such tricks reflect a language's
    weakness in its abstraction capability. Fortunately that's fixed. Let's
    hope to get polymorphic lambdas and concepts in the next iteration:)

    --
    http://kaba.hilvi.org
    Kaba, Aug 16, 2012
    #6
  7. Rui Maciel

    Rui Maciel Guest

    Luca Risolia wrote:

    > Sometimes I used it to dynamically initialize some static data before
    > main(), for example:
    >
    > // .h
    > struct S {
    > static void compute() { // non-const expr
    > // ...
    > }
    > static int x[], y[];
    > static bool init;
    > };
    >
    > // .cpp
    > int S::x[100], y[100];
    > bool S::init = (S::compute(), true);


    Nice one. It also looks like a nifty way to add some semantics to an
    assignment.


    Rui Maciel
    Rui Maciel, Aug 16, 2012
    #7
  8. On 8/16/2012 1:45 PM, Luca Risolia wrote:
    > On 16/08/2012 18:20, Rui Maciel wrote:
    >> Did anyone ever used the comma operator in some place other than a loop
    >> condition?

    >
    > Sometimes I used it to dynamically initialize some static data before
    > main(), for example:
    >
    > // .h
    > struct S {
    > static void compute() { // non-const expr
    > // ...
    > }
    > static int x[], y[];
    > static bool init;
    > };
    >
    > // .cpp
    > int S::x[100], y[100];
    > bool S::init = (S::compute(), true);


    Seems that if you just make your 'compute' return a bool, then there is
    no need in the comma:

    bool S::init = S::compute();

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 16, 2012
    #8
  9. Scott Lurndalæ–¼ 2012å¹´8月17日星期五UTC+8上åˆ1時10分50秒寫é“:
    > Rui Maciel <> writes:
    >
    > >Did anyone ever used the comma operator in some place other than a loop

    >
    > >condition?

    >
    >
    >
    > Bjarne Stroustrup used it extensively in the C-code generated by cfront.
    >
    >
    >
    > http://en.wikipedia.org/wiki/Cfront
    >
    >
    >
    > Very extensively, to the point that it exposed various bugs in PCC on certain
    >
    > architectures. In a box somewhere, I still have a diagram of an expression
    >
    > tree generated by PCC when compiling cfront-generated code. PCC would run out
    >
    > of temporary registers (on a motorola 88100) trying to generate code for the
    >
    > expression (I used sethi-ullman numbers to fix this).
    >
    >
    >
    > http://en.wikipedia.org/wiki/Portable_C_Compiler
    >
    >
    >
    > I still use it to group related items.
    >
    >
    >
    > example:
    >
    >
    >
    > len = snprintf(bp, remaining, "fmt", ...);
    >
    > bp += len, remaining -= len;
    >
    >
    >
    > scott


    I'll vote for the old style main(int argc, void** argv) for
    varried number of arguments.

    But the callee has to know the rules to get the
    varied number of terms from the caller.
    88888 Dihedral, Aug 17, 2012
    #9
  10. Rui Maciel

    Luca Risolia Guest

    On 16/08/2012 23:33, Victor Bazarov wrote:
    > On 8/16/2012 1:45 PM, Luca Risolia wrote:
    >> On 16/08/2012 18:20, Rui Maciel wrote:
    >>> Did anyone ever used the comma operator in some place other than a loop
    >>> condition?

    >>
    >> Sometimes I used it to dynamically initialize some static data before
    >> main(), for example:
    >>
    >> // .h
    >> struct S {
    >> static void compute() { // non-const expr
    >> // ...
    >> }
    >> static int x[], y[];
    >> static bool init;
    >> };
    >>
    >> // .cpp
    >> int S::x[100], y[100];
    >> bool S::init = (S::compute(), true);

    >
    > Seems that if you just make your 'compute' return a bool, then there is
    > no need in the comma:


    Of course, if you can and want to change the return type.
    Luca Risolia, Aug 17, 2012
    #10
  11. Rui Maciel

    Guest

    W dniu czwartek, 16 sierpnia 2012 18:20:14 UTC+2 użytkownik Rui Macielnapisał:
    > Did anyone ever used the comma operator in some place other than a loop
    > condition?


    You might want to check SOCI, the database access library for C++:

    http://soci.sourceforge.net/

    This library uses the comma operator to achieve very readable, concise syntax of database interactions.

    --
    Maciej Sobczak * http://www.inspirel.com/
    , Aug 17, 2012
    #11
  12. On Aug 16, 5:20 pm, Rui Maciel <> wrote:

    > Did anyone ever used the comma operator in some place other than a loop
    > condition?



    slightly more helpful asserts, say in a switch

    default:
    assert (("unknown type of thingy", false));

    though perhaps it would be better to make the string
    "known type of thingy"...
    Nick Keighley, Aug 17, 2012
    #12
  13. Rui Maciel

    Rui Maciel Guest

    Nick Keighley wrote:

    > slightly more helpful asserts, say in a switch
    >
    > default:
    > assert (("unknown type of thingy", false));
    >
    > though perhaps it would be better to make the string
    > "known type of thingy"...


    Nice, a poor man's version of C++11's static_assert().


    Rui Maciel
    Rui Maciel, Aug 17, 2012
    #13
  14. Rui Maciel

    Jeff Flinn Guest

    On 8/17/2012 6:11 AM, Rui Maciel wrote:
    > Nick Keighley wrote:
    >
    >> slightly more helpful asserts, say in a switch
    >>
    >> default:
    >> assert (("unknown type of thingy", false));
    >>
    >> though perhaps it would be better to make the string
    >> "known type of thingy"...

    >
    > Nice, a poor man's version of C++11's static_assert().


    In what way? static_assert is a compile time assert, whereas the code
    above is run time assert. Or am I missing something.

    Jeff
    Jeff Flinn, Aug 17, 2012
    #14
  15. On 8/17/2012 4:49 AM, Nick Keighley wrote:
    > On Aug 16, 5:20 pm, Rui Maciel <> wrote:
    >
    >> Did anyone ever used the comma operator in some place other than a loop
    >> condition?

    >
    >
    > slightly more helpful asserts, say in a switch
    >
    > default:
    > assert (("unknown type of thingy", false));


    "type"? More like "value", perhaps. :)

    > though perhaps it would be better to make the string
    > "known type of thingy"...


    We/I usually use

    assert(<whatever_condition> || !"<reason for failing here>");

    (no need for the comma or extra parens). So, in your case you'd do

    default:
    assert(!"unknown type of thingy");

    right?

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 17, 2012
    #15
  16. Rui Maciel

    Rui Maciel Guest

    Jeff Flinn wrote:

    > In what way? static_assert is a compile time assert, whereas the code
    > above is run time assert. Or am I missing something.


    That is true, static_assert() is a compile time assert while the assert()
    macro performs the tests at run time. I was refering to the fact that, as
    the expression passed to assert() is a constant expression, it is always
    evaluated to:

    assert(false);


    Hence, in this case, as we are dealing with a constant condition, that
    particular assert() would be equivalent to:

    static_assert(false,"unknown type of thingy");


    Then, in practical terms, both can present essentially the same behavior.
    There would be important differences on how each one would perform,
    particularly how the compile time version would be triggered when compiling
    while the run time assertion could stay dormant in the code and only be
    triggered if a thread of execution happens to reach a particular statement.
    Yet, as both assertions can be employed to get essentially the same effect,
    hence the "poor man's version" comment.


    Rui Maciel
    Rui Maciel, Aug 17, 2012
    #16
  17. On 8/17/2012 9:38 AM, Rui Maciel wrote:
    > Jeff Flinn wrote:
    >
    >> In what way? static_assert is a compile time assert, whereas the code
    >> above is run time assert. Or am I missing something.

    >
    > That is true, static_assert() is a compile time assert while the assert()
    > macro performs the tests at run time. I was refering to the fact that, as
    > the expression passed to assert() is a constant expression, it is always
    > evaluated to:
    >
    > assert(false);
    >
    >
    > Hence, in this case, as we are dealing with a constant condition, that
    > particular assert() would be equivalent to:
    >
    > static_assert(false,"unknown type of thingy");


    How would it be equivalent? If the compiler encounters *this*
    static_assert, it will stop the compilation. The expression (it's
    actually a macro, I believe) 'assert(false)', is OK, and will be
    compiled and no diagnostic is required.

    > Then, in practical terms, both can present essentially the same behavior.


    No, they don't. 'static_assert' has behavior in the *compile-time*
    where as 'assert' exhibits its in the *run-time*.

    > There would be important differences on how each one would perform,
    > particularly how the compile time version would be triggered when compiling
    > while the run time assertion could stay dormant in the code and only be
    > triggered if a thread of execution happens to reach a particular statement.
    > Yet, as both assertions can be employed to get essentially the same effect,
    > hence the "poor man's version" comment.


    That does not make sense, sorry.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 17, 2012
    #17
  18. Rui Maciel

    Rui Maciel Guest

    Victor Bazarov wrote:

    > How would it be equivalent?


    Equivalent means having the same effect, and assertions are used to assert
    that a specific condition is met. Knowing this, if you wish to assert that
    a given condition is met, and that condition is expressed through a constant
    condition, then, whether you employ

    assert( ("message",condition))

    ....or

    static_assert(condition, "message)


    ....you still get to determine if a condition has been met. That is, you
    still get the same effect from either option. Hence, in the case under
    consideration, they are equivalent.


    >> Then, in practical terms, both can present essentially the same behavior.

    >
    > No, they don't. 'static_assert' has behavior in the *compile-time*
    > where as 'assert' exhibits its in the *run-time*.


    I'm sure you are aware that, in this context, you are nit-picking. I'm sure
    you are quite able to come up with a scenario where both would be
    equivalent, that is, both would have the same effect.


    >> There would be important differences on how each one would perform,
    >> particularly how the compile time version would be triggered when
    >> compiling while the run time assertion could stay dormant in the code and
    >> only be triggered if a thread of execution happens to reach a particular
    >> statement. Yet, as both assertions can be employed to get essentially the
    >> same effect, hence the "poor man's version" comment.

    >
    > That does not make sense, sorry.


    If you read it I'm sure you will understand.


    Rui Maciel
    Rui Maciel, Aug 17, 2012
    #18
  19. Rui Maciel

    army1987 Guest

    On Thu, 16 Aug 2012 17:20:14 +0100, Rui Maciel wrote:

    > Did anyone ever used the comma operator in some place other than a loop
    > condition?


    I've used it a couple times (in C, not C++, though) in preprocessor
    macros. (I think it's more elegant than the do { /* stuff */ } while(0)
    kludge, and doesn't break should I use it in a loop condition later.)


    --
    [ T H I S S P A C E I S F O R R E N T ]
    Troppo poca cultura ci rende ignoranti, troppa ci rende folli.
    -- fathermckenzie di it.cultura.linguistica.italiano
    <http://xkcd.com/397/>
    army1987, Aug 17, 2012
    #19
  20. Rui Maciel

    Luca Risolia Guest

    On 17/08/2012 16:40, Rui Maciel wrote:
    > Victor Bazarov wrote:
    >
    >> How would it be equivalent?

    >
    > Equivalent means having the same effect, and assertions are used to assert
    > that a specific condition is met. Knowing this, if you wish to assert that
    > a given condition is met, and that condition is expressed through a constant
    > condition, then, whether you employ
    >
    > assert( ("message",condition))
    >
    > ...or
    >
    > static_assert(condition, "message)


    Sometimes you may want to run the dynamic assertion once.
    So another possible use of comma is the following:

    static bool run_assertion_once = (assert(something), true);
    Luca Risolia, Aug 17, 2012
    #20
    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. Paul Davis
    Replies:
    7
    Views:
    589
    Paul Davis
    Jul 17, 2003
  2. Derek
    Replies:
    6
    Views:
    362
    Old Wolf
    Apr 13, 2004
  3. Koster

    Yet another comma operator question

    Koster, Jan 16, 2004, in forum: C Programming
    Replies:
    7
    Views:
    372
    Old Wolf
    Jan 18, 2004
  4. Michael Scarlett

    the whys of the comma operator

    Michael Scarlett, Jun 10, 2004, in forum: C Programming
    Replies:
    8
    Views:
    309
    Michael Scarlett
    Jun 19, 2004
  5. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    472
    Barry Schwarz
    Feb 8, 2005
Loading...

Share This Page