3 ms·
The syntax of + and - is rigid in C++, but functions can be written to extend it. This is not new; you are making claims that were not true in the first ISO C++
by kazinator 2y ago
The syntax of + and - is rigid in C++, but functions can be written to extend it. This is not new; you are making claims that were not true in the first ISO C++ in 1998, or its predecessors revisions going quite far before that.
When a is a class type (and more recently this is now supported for enumerations also) then the surface syntax a + b means the same thing as a.operator+(b): invoking the operator + member function of a with argument b.
- Turing_Machine 2y ago> functions can be written to extend it. Unless I'm mistaken (which I may well be), you can't do that at runtime, only at compile time.
- kazinator 2y agoI replied to this not knowing exactly where the goalposts are. Indeed, in C++, our a + b expression is statically analyzed to be working with specific types; the declared types of a and b. When that code is compiled, it will work with no other types. However, if we anticipate that kind of extension, we can have a be a reference to some numeric_base class where operator + is a virtual function. With some help from a bit of platform specific coding, we can have dynamically loaded modules which can provide new classes derived from numeric_base that ca be passed to existing code in the program which uses + on numeric_base.