I have a struct such as:
struct A {
double x,y;
vector<double> vec;
};
I would like to overload operators such as the plus operator so that I can perform operations such as:
A a,b,c,d;
//do work to set up the four structs. Then:
d = a + b + c;
Performance is important because these operations will be performed in 'inner loops' that are run very many times. I am concerned that the line (a + b + c) will be creating temporaries and so running constructors to 'A' unnecessarily. Running C++17, will the compiler definitely use copy elision to avoid creating temporaries? I need to run the line "d=a+b+c" without ever running the constructor to A.
I know that if I could definitely avoid temporaries by writing:
d = a;
d += b;
d += c;
However, practically, I am about to write a lot of code with long, mathematical lines, and it would be much more convenient to be able to write things all in one line (a + b + c), rather than have to break it down into a ton of "+=" lines.
operator+requires a temporary to hold the result ofa + b, then you're still going to be calling constructors.