私のコードでは、演算子< <を使用して変数を取り、それに従って変数を処理するクラスを作成しています。クラス内に入る正確なロジックは、簡潔さのために、明瞭度&のために省略されています。匿名オブジェクトのoperator <<を使用できません
私が直面してる問題は、私は、オブジェクトの匿名のインスタンスを作成し、(< <)オペレータと直接それを使用しようとすると、ほとんどのコンパイラは文句を言うだろうということです - なし(の線に沿って何かを私が理解から「オペレータ< <」)
の一致、直接クラスを呼び出す(TestObject()は)法的な表現であり、それはオペレータに渡される匿名のオブジェクトをインスタンス化する必要があります。
なぜこれがコンパイルされないのか考えてみてください。
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;
}
あなたが最初のソリューションは、明示的なキャストを必要とすることに注意してください、私はそれがVS – Borgleader