I want to overload some operators multiple times and i am not sure how to do it i have a class
for example
int main(){
addClass c1;
addClass c2;
addClass c1 = (c2 + 10 * c2 + 2 * c3)*c3;
}
my problem is that i dont know how to make this many opreator ovrloading, I mean what do i need to return in ordear for it to continue to the next opreator?
I tried that
class addClass
//declration
friend addClass & operator*(const double, const addClass &);
addClass & operator*(double x, const addClass & a1)
{
int i;
addClass add(a1.rank);
add.res=x*a1.res
}
return res;
}
but for the next oprators i run into some problems so i wanted to know what to do
Thank you!
The order of evaluation is the same as usual even for overloaded operators, so with this example where I've added printing in the operators ...
#include <iostream>
class addClass {
public:
addClass() : addClass(0) {}
// converting constructor
addClass(int x) : value(x) {}
addClass& operator+=(const addClass& rhs) {
value += rhs.value;
return *this;
}
addClass& operator-=(const addClass& rhs) {
value -= rhs.value;
return *this;
}
addClass& operator*=(const addClass& rhs) {
value *= rhs.value;
return *this;
}
addClass& operator/=(const addClass& rhs) {
value /= rhs.value;
return *this;
}
int getValue() const { return value; }
private:
int value;
};
std::ostream& operator<<(std::ostream& os, const addClass& ac) {
return os << ac.getValue();
}
addClass operator+(addClass lhs, const addClass& rhs) {
std::cout << lhs << '+' << rhs << '=';
lhs += rhs;
std::cout << lhs << '\n';
return lhs;
}
addClass operator-(addClass lhs, const addClass& rhs) {
std::cout << lhs << '-' << rhs << '=';
lhs -= rhs;
std::cout << lhs << '\n';
return lhs;
}
addClass operator*(addClass lhs, const addClass& rhs) {
std::cout << lhs << '*' << rhs << '=';
lhs *= rhs;
std::cout << lhs << '\n';
return lhs;
}
addClass operator/(addClass lhs, const addClass& rhs) {
std::cout << lhs << '/' << rhs << '=';
lhs /= rhs;
std::cout << lhs << '\n';
return lhs;
}
int main(){
addClass c1;
addClass c2 = 100;
addClass c3 = 2;
c1 = (c2 + 10 * c2 / 2 * c3)*c3;
}
... you would get this output:
10*100=1000
1000/2=500
500*2=1000
100+1000=1100
1100*2=2200
Related
I received the following error message when trying to output the results of the operator overload *=
no operator "*=" matches these operands
operand types are: ifs::units::Feet *= ifs::units::Feet
This part of the cout statement generated the error
(comp19 *= comp20)
I understand that this is not allowed, but I don’t know how to get the output generated with the *= operator. This test works for += but not for *=, most likely because rhs is a double in this case, but I am not allowed to change this to be an Ftr type.
How can I get the result using the given definition ? Any suggestions?
Here are the files:
Ftr.h
#ifndef __FTR_H
#define __FTR_H
#include <cstdint>
namespace fun::calc
{
class Ftr
{
public:
Ftr() = delete;
explicit Ftr(double Ftr) noexcept;
~Ftr() noexcept;
Ftr(const Ftr &other) = default;
Ftr(Ftr &&other) = default;
double getNum() const noexcept;
Ftr operator+(const Ftr &rhs) const;
Ftr& operator+=(const Ftr &rhs);
Ftr& operator*=(const double &rhs);
private:
double m_Num;
};
}
#endif;
Ftr.cpp
#include "Ftr.h"
#include <iostream>
#include <string>
namespace fun::calc
{
// FUNCTION: Destructor
Ftr::~Ftr() {
}
// FUNCTION: Constructor
Ftr::Ftr(double Ftr) noexcept {
}
// FUNCTION: getNum
double Ftr::getNum() const noexcept {
return m_Num;
}
// FUNCTION: Overloaded Addition
Ftr Ftr::operator+(const Ftr & rhs) const {
return Ftr(m_Num + rhs.m_Num);
}
// FUNCTION: Overloaded Multiplication
Ftr& Ftr::operator*=(const double & rhs) {
m_Num *= rhs;
return *this;
}
}
MainProject.cpp
#include "pch.h"
#include "Ftr.h"
#include <iostream>
#include <string>
using namespace fun::calc;
int main()
//mainFtrTest
{
std::cout << "Ftr Class Testing Started ! \n\n";
Ftr c(4);
std::cout << "Ftr = " << c.getNum() << "\n\n";
// Addition operator
Ftr add1(8);
Ftr add2(7);
Ftr total = add1 + add2;
std::cout << add1.getNum() << " + " << add2.getNum() << " = " << total.getNum() <<"\n\n";
// Multiplication operator
Ftr comp19(9);
Ftr comp20(7);
std::cout << comp19.getNum() << " *= " << comp20.getNum() << " = " << (comp19 *= comp20);
return 0;
}
Ftr& Ftr::operator*=(const double & rhs) means the right hand side of the *= operator should be a double. However, in your main func, you are doing comp19 *= comp20 where the right hand side is a Ftr object. You can do comp19 *= comp20.getNum() to make your right hand side a double, but I think what you really want is to declare:
Ftr& operator*=(const Ftr& rhs);
and define:
Ftr& Ftr::operator*=(const Ftr& rhs){
m_Num *= rhs.m_Num;
return *this;
}
This will make the *= work, but you have another issue at:
<< (comp19 *= comp20);
The right hand side of the << operator is an Ftr object. You need another overload:
std::ostream& operator<<(std::ostream& os, const fun::calc::Ftr& rhs){
os << rhs.getNum();
return os;
}
This overload cannot go into the class definition, because the left hand side of the << operator is a std::ostream object. You must make it a non-member function instead.
Having trouble with the overloaded IOstream in my C++ class, the code below is my header file, so there is no main(). The overloaded iostream seems to work with simple cin and cout calls, but when put into more complex ones, it throws no match for operato<< and operator>>.
/*
Provide three constructors Complex(a, b), Complex(a), and Complex(). Complex()
creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b.
Also provide the getRealPart() and getImaginaryPart() functions for returning
the real and imaginary part of the complex number, respectively.
*/
/*
Overload the operators +, -, *, /, +=, -=, *=, /=, [ ], unary + and -, prefix ++ and --,
postfix ++ and --, <<, >>. Overload the operators +, -, *, / as nonmember functions.
*/
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
class Complex{
public:
Complex();
Complex(double a);
Complex(double a, double b);
void set_I(double input);
void set_R(double input);
double get_I_comp() const; //I accessor
double get_R_comp() const; // double accessor
double getRealPart();
double getImaginaryPart();
Complex operator+(Complex other);
Complex operator+(double other);
Complex operator-(Complex other);
Complex operator-(double other);
Complex operator*(Complex other);
Complex operator*(double other);
Complex operator/(Complex other);
Complex operator/(double other);
void operator++();
Complex& operator++(int dummy);
void operator+=(Complex other);
void operator+=(double other);
void operator-=(Complex other);
void operator-=(double other);
void operator*=(double other);
void operator*=(const Complex& other);
void operator/=(double other);
void operator/=(const Complex& other);
void operator- ();
void operator+ ();
double& operator[](int index);
Complex& operator<<(const int& intput);
Complex& operator>>(const string& output);
friend ostream& operator<<(ostream& out, Complex& target);
friend istream& operator>>(const istream& input, Complex& target);
std::string toString() //temporary solution right now
{
if (this->c_I != 0){
string ret = std::to_string(c_R);
ret = ret + " + ";
ret = ret + std::to_string(c_I);
ret = ret + " i \n";
return ret;
}
else{
string ret = std::to_string(c_R);
return ret;
}
}
Complex& add(double num);
Complex& add(Complex other);
Complex& subtract(double num);
Complex& subtract(Complex other);
Complex& multiply(double num);
Complex& multiply(Complex other);
Complex& divide(double num);
Complex& divide(Complex other);
Complex& abs();
private:
double c_I;
double c_R;
};
Complex::Complex() : c_I(0),c_R(0){ //works
}
Complex::Complex(double a) :c_I(0),c_R(a){ //works
}
Complex::Complex(double a, double b){ //works // at first I have the i as a and r as b, so thats why is fliped
this->c_I = b;
this->c_R = a;
}
double Complex::get_I_comp() const{
return c_I;
}
double Complex::get_R_comp() const{
return c_R;
}
double Complex::getImaginaryPart(){
return c_I;
}
double Complex::getRealPart(){
return c_R;
}
void Complex::set_I(double input){
c_I = input;
}
void Complex::set_R(double input){
c_R = input;
}
Complex Complex::operator+(Complex other){
Complex ret( (this->c_R + other.get_R_comp() ),(this->c_I + other.get_I_comp()));
return (ret);
}
Complex Complex::operator+(double other){
Complex ret(this->c_R + other,this->c_I);
return ret;
}
Complex Complex::operator-(Complex other){
Complex ret(this->c_R - other.get_R_comp(),this->c_I - other.get_I_comp());
return ret;
}
Complex Complex::operator-(double other){
Complex ret(this->c_R - other,this->c_I);
return ret;
}
Complex Complex::operator*(double other){
Complex ret(this->c_R * other ,this->c_I *other);
return ret;
}
Complex Complex::operator*(Complex other){
if((other.get_I_comp() != 0) && (other.get_R_comp() != 0) ){
Complex ret = other * (this->c_R);
Complex neu(-(other.get_I_comp()*this->c_I),other.get_R_comp()*this->c_I);
return (ret + neu);
}
if((other.get_I_comp() == 0 ) && (other.get_R_comp() != 0)){
Complex ret(this->c_R,this->c_I);
ret = ret * other.get_R_comp();
return ret;
}
else{
Complex ret((-((this->c_I)*other.get_I_comp())),(this->c_R)*other.get_I_comp());
return ret;
}
}
Complex Complex::operator/(double other){
if (other == 0) { // zero division error handler
throw runtime_error("Math error: Can't div by zero\n");
return 1;
}
if(other != 0){
Complex ret(this->c_R/other,this->c_I/other);
return ret;
}
}
//To divide a+bi by c+id we will perform the operation (ac+bd)/(c^2 + d^2) + (bc-ad)/(c^2 + d^2)i.
Complex Complex::operator/(Complex other){
if ((other.get_I_comp() != 0) && (other.get_R_comp() != 0)){
double first = ((this->c_R)*other.get_R_comp() + (this->c_I)*other.get_I_comp())/(other.get_R_comp()*other.get_R_comp() + other.get_R_comp()*other.get_R_comp());
double second = (this->c_I*other.get_R_comp() + c_R*other.get_I_comp())/(other.get_R_comp()*other.get_R_comp() + other.get_I_comp()*other.get_I_comp());
Complex ret(first,second);
return ret;
}
if((other.get_I_comp() == 0 ) && (other.get_R_comp() != 0)){
Complex ret(this->c_R,this->c_I);
ret = ret *(1/other.get_R_comp());
return ret;
}
else{
Complex ret(this->c_R,this->c_I);
Complex neu(1/other.get_I_comp());
ret = ret * neu;
return ret;
}
}
void Complex::operator++(){
c_R++;
}
Complex& Complex::operator++(int dummy){
Complex temp = *this;
++temp;
c_R++;
return temp;
}
void Complex::operator+=(double other){
c_R += other;
}
void Complex::operator+=(Complex other){
c_R += other.get_R_comp();
c_I += other.get_I_comp();
}
void Complex::operator-=(double other){
c_R +=(-1*other);
}
void Complex::operator-=(Complex other){
c_R -= other.get_R_comp();
c_I -= other.get_I_comp();
}
void Complex::operator*=(double other){
Complex& reference = *this; //pass by reference editing
reference = reference* other;
}
void Complex::operator*=(const Complex& rhs){
Complex& reference = *this;
reference = reference * rhs;
}
void Complex::operator/=(double other){
Complex& reference = *this;
reference = reference / other;
}
void Complex::operator/=(const Complex& rhs){
Complex& reference = *this;
reference = reference / rhs;
}
double& Complex::operator[](int index){
if(index <= 1){
return(index == 0 ? c_R : c_I);
}
else{
throw std::out_of_range ("index outta bound");
}
}
void Complex::operator-(){
c_R*=(-1);
c_I*=(-1);
}
void Complex::operator+(){
if(c_R<0){
c_R*=(-1);
}
if(c_I<0){
c_I*=(-1);
}
}
Complex& Complex::add(double num){
Complex& reference = *this;
reference = reference + num;
return reference;
}
Complex& Complex::add(Complex other){
Complex& reference = *this;
reference = reference + other;
return reference;
}
Complex& Complex::subtract(double num){
Complex& reference = *this;
reference = reference - num;
return reference;
}
Complex& Complex::subtract(Complex other){
Complex& reference = *this;
reference = reference - other;
return reference;
}
Complex& Complex::multiply(double num){
Complex& reference = *this;
reference = reference*num;
return reference;
}
Complex& Complex::multiply(Complex other){
Complex& reference = *this;
reference = reference * other;
return reference;
}
Complex& Complex::divide(double num){
Complex& reference = *this;
reference = reference/num;
return reference;
}
Complex& Complex::divide(Complex other){
Complex& reference = *this;
reference = reference/other;
return reference;
}
Complex& Complex::abs(){
Complex& reference = *this;
+reference;
return reference;
}
ostream& operator<<(ostream& out, Complex& target){
out << "Real : ";
out << " " << target.getRealPart();
out << " imaginary :";
out <<target.getImaginaryPart();
return out;
}
istream& operator>>(const istream& input, Complex& target) {
string use;
input>>use;
stringstream convert(use);
int x = 0;
convert>>x;
target.set_R(x);
return input;
}
when doing calls such as
cout << "(" << number1 << ")" << " + " << "(" << number2 << ") = " << (number1 + number2) << endl;
it throws the following exception:
main.cpp:19:69: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘Complex’)
cout << "(" << number1 << ")" << " + " << "(" << number2 << ") = " << (number1 + number2) << endl;
In file included from main.cpp:1:0:
Complex.h:276:10: note: candidate: std::ostream& operator<<(std::ostream&, Complex&)
ostream& operator<<(ostream& out, Complex& target){
You have to overload the following function too!.
ostream& operator<<(ostream& out, Complex&& target){
out << "Real : ";
out << " " << target.getRealPart();
out << " imaginary :";
out <<target.getImaginaryPart();
return out;
}
Non-const references don't bind to temporaries.
So ostream& operator<<(ostream& out, Complex& target) can't be used in code that looks like cout << Complex{1.0} or cout << (complex1 + complex2), because in both cases the second argument is a temporary Complex instance.
A possible fix is to use const references when you don't plan to modify the argument:
ostream& operator<<(ostream& out, Complex const& target)
Another solution (for small objects) is to accept it by-value:
ostream& operator<<(ostream& out, Complex target)
I have this formatted string in an istream.
(5, -4)
Let say :
open parenthesis
an integer number
comma and space
another integer number
close parenthesis
I would like to know what is the best approach to extract both integers and validate the string formatting.
This is in a class like this :
class MyPoint
{
public:
MyPoint() = default;
~MyPoint() = default;
...
friend ostream & operator>>(ostream & lhs, MyPoint const & rhs);
...
private:
int x, y;
};
ostream & operator>>(ostream & lhs, MyPoint const & rhs) {
// ???
}
Many thanks to all.
Here is my header file
#ifndef MYPOINT_H
#define MYPOINT_H
#include <iostream>
using namespace std;
class MyPoint
{
public:
MyPoint() : mX{ 0 }, mY{ 0 } { ; }
MyPoint(int x, int y) : mX{ x }, mY{ y } { ; }
~MyPoint() = default;
int x() const { return mX; }
int y() const { return mY; }
void setX(int x) { mX = x; }
void setY(int y) { mY = y; }
MyPoint operator-() const { return MyPoint(-mX, mY); }
MyPoint operator+(MyPoint rhs) const { rhs.mX += mX; rhs.mY += mY; return rhs; }
MyPoint operator-(MyPoint rhs) const { rhs.mX = mX - rhs.mX; rhs.mY = mY - rhs.mY; return rhs; }
MyPoint operator*(MyPoint rhs) const { rhs.mX *= mX; rhs.mY *= mY; return rhs; }
MyPoint operator/(MyPoint rhs) const { rhs.mX = mX / rhs.mX; rhs.mY = mY / rhs.mY; return rhs; }
MyPoint operator%(MyPoint rhs) const { rhs.mX = mX % rhs.mX; rhs.mY = mY % rhs.mY; return rhs; }
friend MyPoint operator+(int lhs, MyPoint const & rhs);
friend MyPoint operator-(int lhs, MyPoint const & rhs);
friend MyPoint operator*(int lhs, MyPoint const & rhs);
friend MyPoint operator/(int lhs, MyPoint const & rhs);
friend MyPoint operator%(int lhs, MyPoint const & rhs);
friend ostream & operator<<(ostream & lhs, MyPoint const & rhs);
friend istream & operator>>(istream & lhs, MyPoint & rhs);
private:
int mX, mY;
};
#endif //MYPOINT_H
And here my source file
#include "MyPoint.h"
MyPoint operator+(int lhs, MyPoint const & rhs) {
return MyPoint(lhs + rhs.mX, lhs + rhs.mY);
}
MyPoint operator-(int lhs, MyPoint const & rhs) {
return MyPoint(lhs - rhs.mX, lhs - rhs.mY);
}
MyPoint operator*(int lhs, MyPoint const & rhs) {
return MyPoint(lhs * rhs.mX, lhs * rhs.mY);
}
MyPoint operator/(int lhs, MyPoint const & rhs) {
return MyPoint(lhs / rhs.mX, lhs / rhs.mY);
}
MyPoint operator%(int lhs, MyPoint const & rhs) {
return MyPoint(lhs % rhs.mX, lhs % rhs.mY);
}
ostream & operator<<(ostream & lhs, MyPoint const & rhs) {
return lhs << "(" << rhs.mX << "," << rhs.mY << ")";
}
istream & operator >> (istream & lhs, MyPoint & rhs) {
return lhs >> "(" >> rhs.mX >> "," >> rhs.mY >> ")"; // HERE is the compiling error
}
And finally, the tests in the main
MyPoint p1, p2(2, -2);
cout << p1 << endl;
cout << p2 << endl;
With this file, I got this error :
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'const char [2]' (or there is no acceptable conversion)
For situations like this, I've often found it handy to define an overload of operator>> to read a predefined string from a stream:
std::istream &operator>>(std::istream &is, char const *pat) {
char ch;
while (isspace(static_cast<unsigned char>(is.peek())))
is.get(ch);
while (*pat && is && *pat == is.peek() && is.get(ch)) {
++pat;
}
// if we didn't reach the end of the pattern, matching failed (mismatch, premature EOF, etc.)
if (*pat) {
is.setstate(std::ios::failbit);
}
return is;
}
With this, reading your format might look something like this:
istream & operator>>(istream & lhs, MyPoint & rhs) {
return lhs >> "(" >> rhs.x >> "," >> rhs.y >> ")";
}
This will do like most typical overloads and set the stream's fail bit if the pattern you've given isn't matched. As it stands now, each string in the input can be preceded by arbitrary white space (just like conversions for numbers and such).
There is technically a minor bug here: as it stands right now, this uses the global locale's definition of whitespace. To be really correct, it should probably use the definition provided in the locale associated with the input stream.
Also note that I had to change your definition of operator>> bit; in the question it looks like an overload of operator<<, with just those two characters changed to get operator>> instead.
For a quick example:
#include <iostream>
std::istream &operator>>(std::istream &is, char const *pat) {
// implementation above
}
class Point {
int x, y;
friend std::istream &operator>>(std::istream &is, Point &p) {
return is >> "(" >> p.x >>"," >> p.y >> ")";
}
friend std::ostream &operator<<(std::ostream &os, Point const &p) {
return os << "(" << p.x <<", " << p.y << ")";
}
};
int main() {
Point p;
std::cout << "Please enter a point: ";
std::cin >> p;
std::cout << "Thanks. Point: " << p << '\n';
}
Tested with VC++ 2013, VC++ 2015, and g++ 6.1 (but this isn't pushing the limits of compilers at all, so I'd expect it to work fine even with compilers so old they're horribly broken in general (e.g., gcc 2.x or VC++ 6.0).
How could I use operator overloading to add two objects without making it a member of of any object? Is this something to do with insertion operator overloading?
so instead of this, to use something more generic i.e. to be used with any objects?:
sameObjectType operator + ( const sameObjectType &x, const sameObjectType &y ) {
sameObjectType z;
z = x+y;
return z;
}
// I want to do the same for subtraction operatoR
sameObjectType operator - ( const sameObjectType &x, const sameObjectType &y ) {
sameObjectType z;
z = x-y;
return z;
}
You can get the idea from this sample code.
#include <iostream>
class One {
private:
int num1, num2;
public:
One(int num1, int num2) : num1(num1), num2(num2) {}
One& operator += (const One&);
friend bool operator==(const One&, const One&);
friend std::ostream& operator<<(std::ostream&, const One&);
};
std::ostream&
operator<<(std::ostream& os, const One& rhs) {
return os << "(" << rhs.num1 << "#" << rhs.num2 << ")";
}
One& One::operator+=(const One& rhs) {
num1 += rhs.num1;
num2 += rhs.num2;
return *this;
}
One operator+(One lhs, const One &rhs)
{
return lhs+=rhs;
}
int main () {
One x(1,2), z(3,4);
std::cout << x << " + " << z << " => " << (x+z) << "\n";
}
I am trying to do operations between large objects and I experiment with r-value references to avoid temporary object creations.
The experiment is the following code, but the result is not what I expected.
The code:
#include <iostream>
using namespace std;
struct A
{
A() = default;
A(const A& a) { cout << "copy ctor" << endl; }
A(A&& a) { cout << "move ctor" << endl; }
A &operator=(const A& a) { cout << "copy assign" << endl; return *this; }
A &operator=(A&& a) { cout << "move assign" << endl; return *this; }
A &operator*=(double s) { cout << "this = this *= s" << endl; return *this; }
A operator*(double s) const { cout << "A = const this * s" << endl; return *this; }
A &operator+=(const A &b) { cout << "this = this + const A&" << endl; return *this; }
A operator+(const A &b) const { cout << "A = const this + const A&" << endl; return *this; }
A &operator+(A &&b) const { cout << "A&& = const this + A&& --> "; return b += *this; }
};
A &operator+(A &&a, const A &b) { cout << "A&& = A&& + const A& --> "; return a += b; }
A &operator*(A &&a, double s) { cout << "A&& = A&& * s --> "; return a *= s; }
int main()
{
A a,b,c,d;
a = b + a * 4 + /*operator*(static_cast<A&&>(d), 2)*/ d * 2 + (A() + c) * 5;
return 0;
}
The output:
A&& = A&& + const A& --> this = this + const A& // A() + c
A = const this * s // (...) * 5
copy ctor // ???
A = const this * s // d * 2
copy ctor // ???
A = const this * s // a * 4
copy ctor // ???
A&& = const this + A&& --> this = this + const A& // (d*2) + (...)
A&& = const this + A&& --> this = this + const A& // (a*4) + (...)
A&& = const this + A&& --> this = this + const A& // b + (...)
copy assign // a = (...)
What I expect:
A&& = A&& + const A& --> this = this + const A& // A() + c
A&& = A&& * s --> this = this *= s // (...) * 5
A&& = A&& * s --> this = this *= s // (...) * 2 d is not used anymore, so I want to move semantics
A = const this * s // a * 4 a is not used anymore, but I want to keep semantics
A&& = A&& + const A& --> this = this + const A& // (d*2) + (...)
A&& = A&& + const A& --> this = this + const A& // (a*4) + (...)
A&& = A&& + const A& --> this = this + const A& // b + (...)
move assign // a = (...)
Here's a more correct version with fewer copies:
#include <iostream>
#include <utility>
using namespace std;
struct A
{
A() = default;
A(const A& a) { cout << "copy ctor" << endl; }
A(A&& a) { cout << "move ctor" << endl; }
A &operator=(const A& a) { cout << "copy assign" << endl; return *this; }
A &operator=(A&& a) { cout << "move assign" << endl; return *this; }
A &operator*=(double s) { cout << "this *= s" << endl; return *this; }
A &operator+=(const A &b) { cout << "this += const A&" << endl; return *this; }
};
A&& operator+(A &&a, const A &b)
{ cout << "A&& + const A&" << endl; a+=b; return std::move(a); }
A&& operator+(A &&a, A &&b)
{ cout << "A&& + A&&" << endl; a+=b; return std::move(a); }
// I assume commutativity
A&& operator+(const A &a, A &&b)
{ cout << "const A& + A&&" << endl; b+=a; return std::move(b); }
A operator+(const A &a, const A &b)
{ cout << "const A& + const A&" << endl; A r(a); r+=b; return r; }
A&& operator*(A &&a, double s)
{ cout << "A&& * s" << endl; a*=s; return std::move(a); }
A operator*(const A& a, double s)
{ cout << "const A& * s" << endl; A r(a); r*=s; return r; }
int main()
{
A a,b,c,d;
a = b + a * 4 + d * 2 + (A() + c) * 5;
return 0;
}
and here's the (annotated) output with ts being temporaries created:
expression level actual operations
---------------- -----------------
const A& * s t1 = a * 4
copy ctor create t1 = copy a
this *= s t1 *= 4
const A& + A&& b + t1
this += const A& t1 += b
const A& * s t2 = d * 2
copy ctor create t2 = copy d
this *= s t2 *= 2
A&& + A&& t1 + t2
this += const A& t1 += t2
A&& + const A& A() + c (note: A() is already a temporary)
this += const A& A() += c
A&& * s A'() * 5
this *= s A'() *= 5
A&& + A&& t1 + A''()
this += const A& t1 += A''()
move assign a = t1 a = t1
I don't think you can expect it any better than just two temporaries for the whole expression.
Concerning your commented-out code: try std::move(d) instead of plain d and you will safe the copy of d in the above output and reduces the number of temporaries to one. If you also add std::move(a), the whole expression is evaluated without a single temporary!
Also note that without std::move(d) and std::move(a), the compiler has no clue that it should/could move those objects, so any code which ends up moving them anyways is dangerous and plain wrong.
Update: I turned my ideas into a library, find it at GitHub. With this, your code becomes as simple as:
#include <iostream>
using namespace std;
#include <df/operators.hpp>
struct A : df::commutative_addable< A >, df::multipliable< A, double >
{
A() = default;
A(const A& a) { cout << "copy ctor" << endl; }
A(A&& a) { cout << "move ctor" << endl; }
A &operator=(const A& a) { cout << "copy assign" << endl; return *this; }
A &operator=(A&& a) { cout << "move assign" << endl; return *this; }
A &operator*=(double s) { cout << "this *= s" << endl; return *this; }
A &operator+=(const A &b) { cout << "this += const A&" << endl; return *this; }
};
while still being efficient and avoiding any unneccesary temporaries. Enjoy!
First of all A() + c returns by lvalue reference. That makes the expression itself an lvalue.
A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.
An lvalue can't bind to an rvalue reference, so the member version of operator* is chosen. Your non-member functions should probably be returning by value:
A operator+(A &&a, const A &b) { cout << "A&& = A&& + const A& --> "; return a += b; }
A operator*(A &&a, double s) { cout << "A&& = A&& * s --> "; return a *= s; }
This causes the result to continue to be a prvalue expression referring to a temporary object.
Second, the copy constructor calls are caused by the member operators returning by value. This will cause a copy of the object. For example, when (...) * 5 returns, it will copy the value of *this out of the function:
A operator*(double s) const { cout << "A = const this * s" << endl; return *this; }
Your operators are implemented to return by value / by lvalue reference.
This results in chained operations accepting either object copy (hence the copy ctor) or lvalue reference.
E.g. b + a * 4 is equal to b.operator+(a.operator*(4)). The input to operator+ will be copy of the object.
Here is the signatures of your methods:
struct A
{
A() = default;
A(const A& a);
A(A&& a);
A &operator=(const A& a);
A &operator=(A&& a);
A &operator*=(double s);
A operator*(double s) const;
A &operator+=(const A &b);
A operator+(const A &b) const;
A &operator+(A &&b) const;
};
A &operator+(A &&a, const A &b);
A &operator*(A &&a, double s);
Problems show up right here. First, free operator+ should return an A&& that it is passed in, to avoid changing the rvalue reference into an lvalue. The same is true of A &A::operator+(A &&b) const; -- it should return an A&&.
Next, your free operators are chaining into the += operators. Here is a cute technique:
template<typename T>
A&&operator+(A &&a, T&&b){ return std::move(a+=std::forward<T>(b)); }
template<typename T>
A&&operator*(A &&a, T&&b){ return std::move(a*=std::forward<T>(b)); }
where we blinding forward our arguments to the += operation.
This can be made more robust, error-wise, with the auto return value technique:
template<typename T>
auto operator+(A &&a, T&&b)->declval(std::move(a+=std::forward<T>(b)))
{ return std::move(a+=std::forward<T>(b)); }
template<typename T>
auto operator*(A &&a, T&&b)->declval(std::move(a*=std::forward<T>(b)))
{ return std::move(a*=std::forward<T>(b)); }
which bumps errors up 1 step in the parsing stack using SFINAE. (Note that the && in T&& and A&& have completely different meanings -- T&&'s && is being used in a type deduction context, so T can bind to any reference type, while A&&'s && is not being used in a type deduction context, so it means A&& binds to an rvalue.).
What follows next is a far more heavily marked up version with some basic modifications for both correctness and efficiencies sake. I keep track of the history of each instance in the name field -- manipulations of this field are not "real", and its value represents the "calculation" required to create a given instance.
I assume move operations move this state.
#include <iostream>
#include <utility>
struct A;
A &operator+=(A& a, std::string op);
A&&operator+=(A&& a, std::string op);
struct recurse_nl {
int& count() {
static int v = 0;
return v;
}
recurse_nl(){if (++count()>1) std::cout << " --> "; else if (count()>2) std::cout << " --> [";}
~recurse_nl(){if (--count() == 0) std::cout <<"\n"; else if (count()>1) std::cout << "]"; }
};
struct A
{
std::string name;
A() = delete;
A(std::string n):name(n) { recurse_nl _; std::cout << "AUTO ctor{"<<name<<"}";};
A(const A& o):name(o.name+"_c&") { recurse_nl _; std::cout << "COPY ctor{"<<name<<"}(const&)"; }
A(A&& o):name(std::move(o.name)) { recurse_nl _; std::cout << "ctor{"<<name<<"}(&&)"; }
A(A& o):name(o.name+"_&") { recurse_nl _; std::cout << "COPY ctor{"<<name<<"}(&)"; }
A &operator=(const A& rhs) { recurse_nl _; std::cout << "COPY assign{"<<name<<"}={"<<rhs.name<<"}"; this->name = rhs.name; return *this; }
A &operator=(A&& rhs) { recurse_nl _; std::cout << "move assign{"<<name<<"}={"<<rhs.name<<"}"; this->name = std::move(rhs.name); return *this; }
A &operator*=(double d) { recurse_nl _; std::cout << "this{"<<name<<"} *= s{"<<d<<"}"; return (*this) += "(*#)"; }
A operator*(double d) const { recurse_nl _; std::cout << "A = const this{"<<name<<"} * s{"<<d<<"}"; A tmp(*this); return std::move(tmp*=d); }
A &operator+=(const A &rhs) { recurse_nl _; std::cout << "this{"<<name<<"} += const A&{"<<rhs.name<<"}"; return ((*this)+="(+=")+=rhs.name+")"; }
A operator+(const A &rhs) const { recurse_nl _; std::cout << "A = const this{"<<name<<"} + const A&{"<<rhs.name<<"}"; return std::move(A(*this)+="(+)"); }
A&& operator+(A &&rhs) const { recurse_nl _; std::cout << "A&& = const this{"<<name<<"} + A&&{"<<rhs.name<<"}"; return std::move(rhs += *this); }
~A() { recurse_nl _; std::cout << "dtor{"<<name<<"}"; }
};
A &operator+=(A& a, std::string op)
{ a.name+=op; return a; }
A&&operator+=(A&& a, std::string op)
{ a.name+=op; return std::move(a); }
template<typename T>
struct ref_type_of {
std::string value() const { return "value"; }
};
template<typename T>
struct ref_type_of<T&> {
std::string value() const { return "&"; }
};
template<typename T>
struct ref_type_of<T&&> {
std::string value() const { return "&&"; }
};
template<typename T>
struct ref_type_of<T const&&> {
std::string value() const { return " const&&"; }
};
template<typename T>
struct ref_type_of<T const&> {
std::string value() const { return " const&"; }
};
template<typename T>
std::string ref_type() { return ref_type_of<T>().value(); }
template<typename T>
A&& operator+(A &&a, T&& b) { recurse_nl _; std::cout << "A&&{"<<a.name<<"} = A&&{"<<a.name<<"} + T" << ref_type<T>(); return std::move(a += std::forward<T>(b)); }
template<typename T>
A&& operator*(A &&a, T&& b) { recurse_nl _; std::cout << "A&&{"<<a.name<<"} = A&&{"<<a.name<<"} * T" << ref_type<T>(); return std::move(a *= std::forward<T>(b)); }
void test1()
{
A a("a"),b("b"),c("c"),d("d");
a = b + a * 4 + d * 2 + (A("tmp") + c) * 5;
}
int main()
{
std::cout << "test1\n";
test1();
return 0;
}
I played with this on live work space and here is the output:
stdout:
test1
AUTO ctor{a}
AUTO ctor{b}
AUTO ctor{c}
AUTO ctor{d}
AUTO ctor{tmp}
A&&{tmp} = A&&{tmp} + T& --> this{tmp} += const A&{c}
A&&{tmp(+=c)} = A&&{tmp(+=c)} * Tvalue --> this{tmp(+=c)} *= s{5}
A = const this{d} * s{2} --> COPY ctor{d_c&}(const&) --> this{d_c&} *= s{2} --> ctor{d_c&(*#)}(&&) --> dtor{}
A = const this{a} * s{4} --> COPY ctor{a_c&}(const&) --> this{a_c&} *= s{4} --> ctor{a_c&(*#)}(&&) --> dtor{}
A&& = const this{b} + A&&{a_c&(*#)} --> this{a_c&(*#)} += const A&{b}
A&&{a_c&(*#)(+=b)} = A&&{a_c&(*#)(+=b)} + Tvalue --> this{a_c&(*#)(+=b)} += const A&{d_c&(*#)}
A&&{a_c&(*#)(+=b)(+=d_c&(*#))} = A&&{a_c&(*#)(+=b)(+=d_c&(*#))} + Tvalue --> this{a_c&(*#)(+=b)(+=d_c&(*#))} += const A&{tmp(+=c)(*#)}
move assign{a}={a_c&(*#)(+=b)(+=d_c&(*#))(+=tmp(+=c)(*#))}
dtor{a}
dtor{d_c&(*#)}
dtor{tmp(+=c)(*#)}
dtor{d}
dtor{c}
dtor{b}
dtor{a_c&(*#)(+=b)(+=d_c&(*#))(+=tmp(+=c)(*#))}
which is pretty verbose, but demonstrates pretty much every operation.
I modified your code so that operator+ and operator* actually creates a new object when required. The expensive operations (creating a new object, and copying) are highlighted through use of AUTO and COPY -- as you can see, there are the initial 4 alphabet objects, the tmp object in the expression, and two copies created by operator*(double).
We can get rid of some of the copies with this:
a = b + std::move(a) * 4 + std::move(d) * 2 + (A("tmp") + c) * 5;
however, we still end up with 3 objects with non-trivial state to destroy because twice we do operator+(A&&, A&&), and I didn't assume that this operation is extra efficient.
If it is, we can add this operator:
A &operator+=(A &&rhs) { recurse_nl _; std::cout << "this{"<<name<<"} += A&&{"<<rhs.name<<"}"; return ((*this)+="(+=")+=std::move(rhs.name)+")"; }
and the resulting output shows that only one object with non-trivial state is ever destroyed.
Final version on live workspace is here.
(The recurse_nl object is for recursion tracking. At the base level, it prints a newline at the end of the function. At deeper recursions, it does the --> printing, and in theory if recursion got deep enough it would print [ brackets to help).
Final output:
test1
AUTO ctor{a}
AUTO ctor{b}
AUTO ctor{c}
AUTO ctor{d}
AUTO ctor{tmp}
A&&{tmp} = A&&{tmp} + T& --> this{tmp} += const A&{c}
A&&{tmp(+=c)} = A&&{tmp(+=c)} * Tvalue --> this{tmp(+=c)} *= s{5}
A&&{d} = A&&{d} * Tvalue --> this{d} *= s{2}
A&&{a} = A&&{a} * Tvalue --> this{a} *= s{4}
A&& = const this{b} + A&&{a(*#)} --> this{a(*#)} += const A&{b}
A&&{a(*#)(+=b)} = A&&{a(*#)(+=b)} + Tvalue --> this{a(*#)(+=b)} += A&&{d(*#)}
A&&{a(*#)(+=b)(+=d(*#))} = A&&{a(*#)(+=b)(+=d(*#))} + Tvalue --> this{a(*#)(+=b)(+=d(*#))} += A&&{tmp(+=c)(*#)}
move assign{a(*#)(+=b)(+=d(*#))(+=tmp(+=c)(*#))}={a(*#)(+=b)(+=d(*#))(+=tmp(+=c)(*#))}
dtor{}
dtor{}
dtor{c}
dtor{b}
dtor{a(*#)(+=b)(+=d(*#))(+=tmp(+=c)(*#))}
where you can see the single "complex object" destroyed at the end (together with its entire history).