5

在下面的示例代码中,它表明 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++ 说这些是模棱两可的,即使第一个的最差转换比第二个的最差转换更好:

4

3 回答 3

4

问题不在于元组,而在于您的运算符。这很好用:

ostream& operator<<(ostream& os_, Mytuple tuple_)
{
    os_ << 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_;
}

问题是 ostringstream 继承operator<<自 ostream,它具有此签名 : ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)是允许的。然后

ostream& operator<<(ostream& os, T t)

(用 c++ 中的所有可用类型更改 T,请参阅operator<< 参考页

编辑

这是一个简化的示例(没有元组):

ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)
{
    const int i = tuple_.get<0>();
    os_ << i; // error in this line
    return os_;
}

现在的错误是:

dfg.cpp: In function ‘std::ostringstream& operator<<(std::ostringstream&, Mytuple)’:
dfg.cpp: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:
/usr/lib/gcc/i386-redhat-linux/4.3.0/../../../../include/c++/4.3.0/bits/ostream.tcc:111: note: candidate 1: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
dfg.cpp:14: note: candidate 2: std::ostringstream& operator<<(std::ostringstream&, Mytuple)

上面的错误信息说:无法在两个运算符operator<<(ostream&,...)operator<<(ostringstream&,...). This also raises another question : why on earth do you need运算符<<(ostringstream&,...)`之间进行选择?

于 2011-09-29T07:35:21.287 回答
3

当你写

 os << tuple_.get<0>();

没有与这两个参数匹配的函数。相反,编译器可以选择对任一参数应用隐式转换

std::ostream << int

或者

std::ostringstream << MyTuple

后者将发生在构造函数中,该boost::tuple构造函数可以接受任意数量的参数,最多可包含 tuple 元素的数量。(并且float它失败了,因为float可以转换为int。)

重载流运算符时,将基类用作左侧(ostream甚至basic_ostream<CharT, Traits>.


编辑:您可以通过转换第一个参数来消除呼叫的歧义。

ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)
{
  static_cast<std::ostream&>(os_) << tuple_.get<0>(); 
  static_cast<std::ostream&>(os_)  << tuple_.get<1>();      
  static_cast<std::ostream&>(os_)  << tuple_.get<2>();      // Error because no matching operator. Fine.
  return os_;
}

但是,重载运算符ostringstream仍然不是一个好主意,因为它不适用于运算符链接。

MyTuple a, b;
ostringstream ss;
ss << a << ' ' << b;

将调用:

1)ostringstream& operator<<(ostringstream& os_, Mytuple tuple_)

2)ostream& ostream::operator<<(char)

3)ostream& operator<<(ostream&&, boost::tuple<int,float,Myclass>

于 2011-09-29T08:05:31.150 回答
1

所有那些告诉你使用::std::ostream类型而不是类型的人::std::ostringstream都是绝对正确的。你不应该这样使用::std::ostringstream

但我对你的代码的主要不满是令人痛苦的缺乏普遍性。它仅适用于一种特定的元组类型,而不适用于所有类型。

所以我在 C++0x 中编写了一个operator <<for ,它适用于任何可以使用. 它可能可以相对容易地转换为与 Boost 的元组类型一起使用。这里是:::std::tupleoperator <<

template < ::std::size_t fnum, typename tup_type>
void print_fields(::std::ostream &os, const tup_type &val)
{
   if (fnum < ::std::tuple_size<tup_type>::value) {
      ::std::cerr << "Fred " << fnum << '\n';
      os << ::std::get<fnum, tup_type>(val);
      if (::std::tuple_size<tup_type>::value > (fnum + 1)) {
         os << ", ";
      }
      print_fields<fnum + 1, tup_type>(os, val);
   }
}

template < ::std::size_t fnum, typename... Elements>
class field_printer;

template <typename... Elements>
class field_printer<0, Elements...> {
 public:
   typedef ::std::tuple<Elements...> tup_type;

   static void print_field(::std::ostream &os, const tup_type &val) {
   }
};

template < ::std::size_t fnum, typename... Elements>
class field_printer {
 public:
   typedef ::std::tuple<Elements...> tup_type;

   static void print_field(::std::ostream &os, const tup_type &val) {
      constexpr auto tupsize = ::std::tuple_size<tup_type>::value;
      os << ::std::get<tupsize - fnum, Elements...>(val);
      if (fnum > 1) {
         os << ", ";
      }
      field_printer<fnum - 1, Elements...>::print_field(os, val);
   }
};

template <class... Types>
::std::ostream &operator <<(::std::ostream &os, const ::std::tuple<Types...> &val)
{
   typedef ::std::tuple<Types...> tup_type;
   os << '(';
   field_printer< ::std::tuple_size<tup_type>::value, Types...>::print_field(os, val);
   return os << ')';
}

这会将元组打印为"(element1, element2, ...elementx)".

于 2011-09-30T00:33:16.677 回答