4

The same question goes for | and ||.

Why would one overload or 'use' the & and && operator? The only use i thought of are

  • Bitwise Ands for int base types (but not float/decimals) using &
  • logical short circuit for bools/functions that return bool. Using the && operator usually.

I cant think of any classes that use those operators. Absolutely none. I know a class might support + (and not '-') which combine two strings together. I seen an object such as datetime overload '-' so two dates can be subtracted to make a timespan (obviously you cant add two dates) but i never seen &, &&, | and || used.

Does anyone know of a use? In any language?

1
  • 4
    A custom "many, many bits" type?
    – user1249
    Commented Mar 6, 2011 at 11:23

7 Answers 7

12

Basically, any class that has set semantics has a use for & - for example, sets themselves, bit vectors etc. Any class that has logic semantics has a use for && - fuzzy logic, k-ary logic, etc.

6

IMO it makes some sense to overload the & operators for Set types; resulting in the intersection of the two sets. Likewise for the | operator, which would result in the union of the two sets.

5
  • What should + be for a set type?
    – user2528
    Commented Mar 6, 2011 at 10:29
  • 3
    acidzombie: actually, there is no good definition of + for a set, though one might be tempted to use it for union; but obviously A+B-B=A doesn't hold (neither does A-B+B=A)
    – user281377
    Commented Mar 6, 2011 at 17:03
  • You could use + for a special union that keeps duplicates. Then - would be straightforward and A+B-B=A would indeed hold. Of course, you probably lose some nice set properties there. Is that what a multiset is? Hmm. Commented Mar 6, 2011 at 17:41
  • 1
    Matthew Read: That would be a multiset, indeed, but A-B+B=A still doesn't hold, unless you allow negative multiplicities.
    – user281377
    Commented Mar 6, 2011 at 17:42
  • 1
    This is exactly what the Python set type does. It also uses ^ for symmetric difference.
    – dan04
    Commented Mar 6, 2011 at 19:27
3

Note:

In C++ you should not overlaod the && and || operators. The overloaded versions are not shortcut operators anymore. overloaded operators are syntactic sugar for function calls and as such all parameters are evaluated before the call.

Talking about C++:
As with all user definable overloads you can make them do anything; its totally up to the developer. Unfortunately that is also their downfall. Overloading operators to do non-standard operations confuses people and makes the code harder to maintain.

The C++ << , >> operators spring to mind. Not only are they bit shift operators but in C++ they have been overloaded to be stream input/output operators. To the new comer to C++ this usage catches them off guard (and you see the questions hear all the time).

Thus in my opinion you should only overload operators to do exactly what the user expects to be done. Thus '|', '&' should only be overloaded to do a bitwise 'or', 'and'. If this has meaning to your class then fine otherwise you should not overload them.

As for other uses I have seen the '|' operator used as a string concatenation operator.

1

When doing domain driven design and using the specification pattern one must override the logical operators.

Read this nice article on the CodeProject Or watch this screen cast on dimecast

The specification pattern is a great way to make domain logic explicit and more readable for business annalists.

Tip: read this great book on domain driven design

2
  • I looked at this and i watched the dimecast, i am completely confused. Why don't they overload the operators instead of creating many functions and calling them And/Or/Not ? en.wikipedia.org/wiki/Specification_pattern#Example_of_use
    – user2528
    Commented Mar 6, 2011 at 13:38
  • Sorry in my memory they did. I can imagine they didn't because they wanted to create a simpeler example or because they find the methods easier to read in the fluent interface.
    – KeesDijk
    Commented Mar 6, 2011 at 15:16
0

The | operator can be used for shell-like piping, when using a programming language as a "shell" language -- see IPython IPipes for an example of this use.

0

The '&' is sometimes hijacked for other purposes as well.

Example is the boost serialization library, they use it as their in-out function call instead of separate << and >> methods:

ar & data;

Can therefore be used in both seralize and deserialize calls.

1
  • Thats a pretty awesome use.
    – user2528
    Commented Mar 7, 2011 at 15:08
-1

I have at times used them to do operations over user defined classes. Makes reading the code easier.

3
  • Can you give me an example? this is what interest me.
    – user2528
    Commented Mar 6, 2011 at 11:40
  • The above example of sets is one. I work with a lot of signal video manipulation and dealing with that data is much easier if you follow OOD and overload these operators while doing encoding/decoding operations. eg: frame1 & frame2 or frame1 && frame2 for comparison etc. Commented Mar 6, 2011 at 14:03
  • I dont understand, why would you use frame1 '&' or '&&' frame2? what is it suppose to do?
    – user2528
    Commented Mar 6, 2011 at 14:59