在下面的示例代码中,它表明 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;
}
错误信息:
tupleTest2.C:18: error: ISO C++ 说这些是模棱两可的,即使第一个的最差转换比第二个的最差转换更好: