1

In my code below, I am creating a class that takes in variables using operator<< and processes the variables accordingly. The exact logic that will go inside the class is omitted for simplicity & clarity.

The issue that I'm facing is that when I try to create an anonymous instance of the object, and use that directly with the (<<) operator, most compilers would complain - something along the lines of (no match for 'operator<<')

From what I understand, calling the class directly ( TestObject() ) is a legal expression, and it should instantiate an anonymous object that gets passed into the operator.

Appreciate your thoughts on why this does not compile?

typedef unsigned int uint32;

class TestObject
{
public:
   TestObject()
      : mValue(0)
   {
   }

   uint32 GetValue() const
   {
      return mValue;
   }

private:
   uint32 mValue;
};

template <typename T>
TestObject& operator<<(TestObject& o, const T& t)
{
   return o;
}

void TestObjectTest()
{
   TestObject myTestObject;
   uint32 newValue = 123;
   const uint32 resultValue = (myTestObject << newValue).GetValue(); // This compiles for both visual studio 2013 and gcc.
   const uint32 resultValue2 = (TestObject() << newValue).GetValue(); // Compiles using visual studio 2013, but does not compile using x86-64 gcc 6.2: "no match for 'operator<<' in 'TestObject() << newValue'
}

int main(void)
{
    TestObjectTest();
    return 0;
}
ShErbO
  • 37
  • 3

1 Answers1

3

TestObject() yields a temporary TestObject. Since it is not a temporary you cannot bind it to a lvalue reference (except for MSVS's evil extension). If your operator does not need to modify the TestObject then simply changing it to take a const& is enough:

template <typename T>
const TestObject& operator<<(const TestObject& o, const T& t)
{
   return o;
}

If you need to modify the value then you need to add another overload and take in a rvalue reference. That will bind to the temporary and allow you to modify it:

template <typename T>
TestObject& operator<<(TestObject&& o, const T& t)
{
   return o;
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402