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;
}