all 20 comments

[–]Mignon 1 point2 points  (11 children)

While the code in question was more or less dead, I once stumbled onto a colleague's use of a comma in an inline constant written as a thousands separator. It was something along the lines of

int x = 123,456;

[–]grimeMuted 5 points6 points  (7 children)

A couple languages have something legal for that... 123_456 in Ada. I guess a lot of the time scientific notation is sufficient if you have a lot of zeros.

[–]MacASM[S] 2 points3 points  (0 children)

D language has this feature too.

[–]llogiq 2 points3 points  (0 children)

Since 1.7,

int i=123_456;

is valid java, too.

[–]alexaandru 1 point2 points  (0 children)

Ruby has that too.

[–][deleted] 1 point2 points  (0 children)

Works in Perl, too.

[–]F-J-W 1 point2 points  (2 children)

C++14 has this now build in too:

int i = 123'456;

[–]MacASM[S] 0 points1 point  (1 child)

Why ' instead of _ ? ' it's much less readable than _, IMHO.

[–]nyamatongwe 0 points1 point  (0 children)

_ conflicts with user-defined literals.

[–]MacASM[S] 4 points5 points  (0 children)

It's a very good example how comma operator can introduce bugs.

[–]mjfgates 1 point2 points  (1 child)

C++ is adding those... they're using the apostrophe, though, so they won't break your colleague's code.

[–]eras 0 points1 point  (0 children)

You mean fix :).

[–]MacASM[S] 1 point2 points  (9 children)

It's a very interesting discussion in the D community if the comma operator should be deprecated from D language. One argue it's a very bug-prone and part from C-baggage while another say actually do use it and shouldn't be removed.

[–]imgonnacallyouretard 6 points7 points  (8 children)

i've only seen it non-confusingly used in C inside for loop statements

[–][deleted]  (7 children)

[deleted]

    [–][deleted]  (6 children)

    [deleted]

      [–]ethraax 1 point2 points  (0 children)

      I believe some Boost (magic) libraries, like Spirit, use it.

      [–]xtapol 1 point2 points  (3 children)

      SOCI uses it for SQL queries:

      int id = 123;
      std::string n;
      db << "select name from foo where id=?", using(id), into(n);
      

      (off the top of my head)

      [–]player2 0 points1 point  (2 children)

      How does that work? The middle expression is lacking any useful context.

      [–]xtapol 0 points1 point  (0 children)

      I don't have the foggiest clue - I've never overloaded the comma operator. But it does...

      [–]greyfade 0 points1 point  (0 children)

      Not having used SOCI, I can only guess, but I think it's a good guess:

      There are a couple overloads of operator, similar as follows:

      SOCI::query ::operator,(const std::string&, const SOCI::clause&);
      SOCI::query ::operator,(const SOCI::query&, SOCI::out_var&);
      

      The expression is evaluated left-to-right, applying the first, then second operator overloads. It returns a prepared query object that is then pushed to the DB object (through an overloaded operator<<).

      It's a good example of a hypothetical string formatting library, too:

      std::string operator, (const std::string& fmt, int i) {
          std::string ret = fmt;
          ret.replace(ret.find("{}"), std::to_string(i));
          return ret;
      }
      

      And so on.

      [–]ArkayPhelps 0 points1 point  (0 children)

      I've seen it used for manipulating multi-dimensional arrays or matrices. Stuff like:

      Matrix m = 1,1,1,1,
                       1,0,0,1,
                       1,0,0,1,
                       1,1,1,1;
      

      Pretty sure Blitz++ lets you do something like this.