3 ms·
In C++, with its templates, there are only a couple alternatives: 1. Operator overloading 2. Operator desugaring (e.g. __subscript__(), which substitutes the
by projektfu 3y ago
In C++, with its templates, there are only a couple alternatives:
1. Operator overloading
2. Operator desugaring (e.g. __subscript__(), which substitutes the intrinsic function for basic types, but can also be defined for user defined types)
3. Writing templates with weird adaptors for primitive types.
Given that its design goal was to embed C, there were already operators that worked with various and mixed types. Adding (+.), etc., would have been unacceptable to the users. So, I think in general, for this language, it was good but, unfortunately, iostream made people think you should overload the behavioral expectation, too.
- duped 3y agoThe alternative GP is advocating for is "none of the above." Meaning "operators are only defined for primitive types" which is perfectly fine when working with C. If you wanted to use an abstraction over non primitive types for such things you would use a normal function.
- c-cube 3y agoBut then you make built-in types special. One of the tenets of C++ is that user defined types have as much power as built-in types.
- duped 3y agoBuilt in types are special. The compiler needs to define operator precedence and certain semantics (commutativity, associativity, overflow, etc) that cannot be expressed in the type system or enforced by the interface. The fact that it makes syntax "nicer" for user defined types is at best subjective and at worst an anti-pattern because it leads to bad compiled code and confused programmers. Function calls are unambiguous and follow the same rules as other function calls, while operator overloading does not.
- pjmlp 3y agoIts design goal was to repeat the author's experience having to downgrade himself from Simula to BCPL, he wasn't keen in repeating the same experience with C, when he went out to write distributed computing applications at Bell Labs. Bjarne has given a couple of interviews on the matter.