Search code examples
c++functionoperator-overloadingcomma-operator

When to Overload the Comma Operator?


I see questions on SO every so often about overloading the comma operator in C++ (mainly unrelated to the overloading itself, but things like the notion of sequence points), and it makes me wonder:

When should you overload the comma? What are some examples of its practical uses?

I just can't think of any examples off the top of my head where I've seen or needed to something like

foo, bar;

in real-world code, so I'm curious as to when (if ever) this is actually used.


Solution

  • Let's change the emphasis a bit to:

    When should you overload the comma?

    The answer: Never.

    The exception: If you're doing template metaprogramming, operator, has a special place at the very bottom of the operator precedence list, which can come in handy for constructing SFINAE-guards, etc.

    The only two practical uses I've seen of overloading operator, are both in Boost: