charlieparker
charlieparker

Reputation: 186

Operator overloading compiles in Visual Studio 2013 but not gcc

I have a class where I am overloading the << and + operators like this:

    friend MyObject operator+(MyObject lhs, MyObject rhs){
        MyObject obj;
        obj.Property = lhs.Property + rhs.Property;
        return obj;
    }

    friend std::ostream& operator<<(std::ostream& os, MyObject& n) {
        os << MyObject.Property;      //property is a double
        return os;
    }

This compiles in both Visual Studio and gnu++98 with no problems. I can also call them like this without any problems:

MyObject obj1, obj2;
obj1.PutInValidState();
cout << obj1;
obj1 = obj1 + obj2;

However, when I call them at the same time like this:

cout << obj1 + obj2;

Visual Studio will compile it (and gives the correct output), but gnu++98 gives me the error "no match for 'operator<<' in ' std::operator<<(std::basic_ostream&, const char*)". I can get it to compile in both by adding const to the parameter in the function definition like this:

friend std::ostream& operator<<(std::ostream& os, const MyObject& n) {

What is going on here? Why do I need the const to make it work in gnu++98 but not in Visual Studio?

Upvotes: 0

Views: 491

Answers (1)

Bwmat
Bwmat

Reputation: 4578

cout << obj1 + obj2;

is syntax sugar for

operator<<(cout, operator+(obj1,obj2));

here you can see that the n parameter in your operator<<() overload is being bound to the value being returned by operator+(), which is a temporary object. Now, in C++, there's a rule that non-const reference parameters can not bind to temporaries, which is why it won't compile in gcc.

Visual studio, on the other hand, has a language extension which allows non-const reference parameters to bind to temporaries. I wish it didn't, since that extension is one of the ones that has bitten me when porting code to other compilers.

Upvotes: 2

Related Questions