以下のサンプルコードでは、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のコンストラクタを持っていますので、あなたは' int'から変換可能なコンストラクタ 'Mytuple(int)'を持っています。 –
これはgcc 4.5とgcc 4.7の実験ではうまくコンパイルできます。どのコンパイラのバージョンを使用していますか? – rodrigo