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:エラー:ISO C ++は、最初の変換の最悪の変換が2番目の変換の最悪の変換よりも優れているにもかかわらず、これらはあいまいであると言っています。

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_;
}

問題は、ostringstreamoperator<<が 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

于 2011-09-29T07:35:21.287 に答える
3

あなたが書くとき

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

両方のパラメーターに一致する関数はありません。代わりに、コンパイラはいずれかのパラメーターに暗黙的な変換を適用する選択肢があります

std::ostream << int

また

std::ostringstream << MyTuple

後者はboost::tuple、タプル要素の数までの任意の数の引数を取ることができるコンストラクターで発生します。(そして、は に変換可能floatであるため、失敗します。)floatint

ストリーム演算子をオーバーロードするときは、基本クラスを左側 (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

しかし、あなたのコードに対する私の主な不満は、一般性の悲惨な欠如です。特定の 1 つのタプル タイプに対してのみ機能し、すべてのタプル タイプに対して機能するわけではありません。

だから私は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 に答える