Overloading Comma Operator

Can anyone please explain to me why and when someone would want to ever overload the comma operator, or was this feature just provided because it was easier to provide than not to provide?

Try asking news:comp.std.c++ why such an immoral action is not illegal.

Can't legislate morality, even in programming :-)

"Stupid is as stupid does." -- ForrestGump?

[CategoryEvil] [CategoryOpinion?]


The comma operator is used to great effect in the BoostLambdaLibrary. Similar use is made in the implementation of lambda operations in FunctoidsInCpp, where it is essential to establishing a notation e.g [x,y] for functoid arguments. There is another example of its use in the CppCookbook (p551) to initialise a sequence with comma separated values. -- JohnFletcher


There is actually one notationally useful example of this in the BlitzPlusPlus library. In numerical programming you often need to specify a small array (matrix), and blitz++ simplifies this by allowing things like:

 Array<int,2>(3,3);
 A = 1, 2, 3,
     4, 5, 6,
     7, 8, 9;
 cout << A << endl;


When above example had
 Array<int,2> A(3, 3, FortranArray<2>());
Can some kind soul please explain to me what the third parameter to the constructor actually means?

Ok, I just CutAndPaste the above example from blitz++ documents, I shouldn't have included that as it doesn't have anything to add to the "," discussion. However, now I have so - the issue is that 2d arrays may be stored in column major or row major order. I.e., the array

 [1 2 3]
 [4 5 6]
may be stored physically as (row major) [1 2 3 4 5 6] or (column major) [1 4 2 5 3 6]. FortranLanguage stores arrays in column major, whilst CeeCeePlusPlus etc. tend to use row major. In numerical work, you may have an algorithm or DataStructure that is more convenient to leave in fortran style, so the blitz container can do both (and more, for higher dimensions).

The array in the example produces the array

 [1 2 3]
 [4 5 6]
 [7 8 9]
but with the Fortran array argument, the array would be

 [1 4 7]
 [2 5 8]
 [3 6 9]
which is confusing...


Is there a way to overload the semicolon operator?

The semicolon isn't an operator, it's a syntactic element, like a quote mark or the leading hash in a preprocessor directive. -- GavinLambert


You may find this an interesting overload of the comma operator... or you may want to hit me over the head... (Not sure this works for compilers other than GnuCpp 3.3.)

  #include <iostream>
  template<class T>
  inline std::ostream& operator,(std::ostream& o, const T& value) {
    o << value;
    return o;
  }
  //Handle manipulators such as std::endl.
  inline std::ostream& operator,(std::ostream& o, std::ostream& (*manip_fun)(std::ostream&)) {
    o << manip_fun;
    return o;
  }
  #define print1(ARGS...) std::cout << ARGS
  #define print2 std::cout,

//Allows for the following two types of statements... print1("abc", ' ', 1 << 2, ' ', 1.6, std::endl); print2 "Or ", 'P', "erl-style.\n";
-- MikeThomson?

Cute, but the vararg macro is not standard CeePlusPlus

Variadic macros [VarArgMacros] are standard for C99. Their support seems fairly common for C++, although the above code uses a GnuCompilerCollection-specific extension to the CeeLanguage99 standard. A quick GoogleSearch suggests they are recommended for C++: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1545.pdf.

See TheCppStandardsCommittee for more on changes to C++.


I'm inordinately proud of my use of OverloadingCommaOperator in CompileTimeGenericAverageFunctionInCeePlusPlus. -- AdamBerger


In SmalltalkLanguage, the comma operator is used to define concatenation for collections. Strings, being a collection of characters, are also concatenated with commas.
There is an example program at http://www.linuxtopia.org/online_books/programming_books/thinking_in_c++/Chapter12_011.html
See also OperatorOverloading, PlusSignOverloading.
CategoryLanguageFeature CategoryCpp

EditText of this page (last edited September 20, 2010) or FindPage with title or text search