## STD-DISCUSSION | |

**Subject:** commutative functions

**From:** Tobias W. (*tobias.wallner1_at_[hidden]*)

**Date:** 2020-08-29 19:03:29

**Next message:**Jeremy Ong: "Re: commutative functions"**Previous message:**Andrew Schepler: "Re: Anonymous unions with non-trivial member types"**Next in thread:**Jeremy Ong: "Re: commutative functions"**Reply:**Jeremy Ong: "Re: commutative functions"**Reply:**Matthew Woehlke: "Re: commutative functions"

Hi everyone!

I am coming with a mathematical and embedded systems background and

there is something that bothered me since I started programming.

Firstly, I would like to explain the problem with an example that occurs

often in slightly different but similar ways.

Secondly, I will propose a possible solution.

Of course, I would love to hear your thoughts.

So let's imagine we have three classes.

Â Â Â Â Â class Real{

Â Â Â Â Â Â Â Â Â Â Â double value;

Â Â Â Â Â Â Â Â Â Â ...

Â Â Â Â Â };

Â Â Â Â Â class Imag{

Â Â Â Â Â Â Â Â Â Â double value;

Â Â Â Â Â Â Â Â Â Â Â ...

Â Â Â Â Â }

Â Â Â Â Â class Complex{

Â Â Â Â Â Â Â Â Â Â Â double Im;

Â Â Â Â Â Â Â Â Â Â Â double Re;

Â Â Â Â Â Â Â Â Â Â ...

Â Â Â Â Â };

Then if I was to write functions, for example, an add function or

operator+ and would like to add them in all possible permutations of

arguments.

This means, that I would have to write all those following functions,

just to add them:

Â Â Â Â Â Complex operator+ (Real a, Imag b);

Â Â Â Â Â Complex operator+ (Imag b, Real a); // Mirrored function

Â Â Â Â Â Complex operator+ (Real a, Complex b);

Â Â Â Â Â Complex operator+ ( Complex b, Real a); //Mirrored function

Â Â Â Â Â Complex operator+ (Imag a, Complex b);

Â Â Â Â Complex operator+ ( Complex b, Imag a); //Mirrored function

From a mathematical point of view, all mirrored functions are obsolete,

because the intent has already been stated in the function above.

Furthermore, each mirrored function will use additional program memory

and this can become an issue on embedded devices.

Therefore I would like to introduce the idea of the expression

"commutative" that can be added to functions.

The commutative expression will tell the compiler that he can switch the

parameters that will be passed to a function.

Following this, the keyword will only be allowed for functions that

possess two and only two parameters.

Then the example from above may look something like this:

Â Â Â Â commutative Complex operator+ (Real a, Imag b);

Â Â Â Â commutative Complex operator+ (Real a, Complex b);

Â Â Â Â commutative Complex operator+ (Imag a, Complex b);

If the compiler comes across a statement like below, he is allowed to

restructure the code.

Â Â Â Complex =Â Complex * Imag + Real;

will change to:

Â Â Â Complex = Real + Imag * Complex;

and now the three commutative functions can be used to process this

command.

This is far less to write, the intent is clear, fewer functions are used

and I only pay for the programme memory I need.

This, of course, is not allowed to change the evaluation order of

operator* and operator+. The operator* will always be evaluated first

and the operator+ will always work with its result.

The operator+ is not allowed to steal one of the operator* 's parameters.

commutative will, of course, be available for all functions that take 2

parameters, not just operators.

These are my thoughts, as already stated above, I would love to hear yours.

Sincerely,

Tobias Wallner

**Next message:**Jeremy Ong: "Re: commutative functions"**Previous message:**Andrew Schepler: "Re: Anonymous unions with non-trivial member types"**Next in thread:**Jeremy Ong: "Re: commutative functions"**Reply:**Jeremy Ong: "Re: commutative functions"**Reply:**Matthew Woehlke: "Re: commutative functions"

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org