在下面的示例代碼中,它顯示boost :: tuple可以從第一個模板參數隱式創建。 因此,我不能寫<<
運營商,因爲它變得模糊。如何爲boost :: tuple寫一個`<<`運算符?
另外我不明白爲什麼ostringstream& << float
也含糊不清。這沒有任何隱含的構造。爲什麼這也會導致模棱兩可的錯誤?
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <sstream>
#include <string>
using namespace std;
class Myclass
{
};
typedef boost::tuple<int,float,Myclass> Mytuple;
ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)
{
float f = tuple_.get<1>();
//os_ << (int)tuple_.get<0>(); // Error because int is implicitly converted into Mytuple. WHYY?
//os_ << tuple_.get<1>(); // No Clue Why this is ambiguous.
//os_ << tuple_.get<2>(); // Error because no matching operator. Fine.
return os_;
}
int main()
{
Mytuple t1;
t1 = 3; // Working because int is implicitly converted into Mytuple!! WHY?
//t1 = 3.0f; // Error because no matching constructor. Fine.
return 0;
}
錯誤Mesasge:
tupleTest2.C:18: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
'boost :: tuple'已經有一個'operator <<',就像那樣。我不明白這是如何導致你得到的錯誤,但可能是相關的。 –
另外'boost :: tuple'具有用於元組元素0..n的構造函數,所以你的構造函數爲'Mytuple(int)',它可以從'int'轉換。 –
它編譯罰款 - 因爲它應該與gcc 4.5和gcc 4.7實驗。你使用什麼編譯器版本? – rodrigo