42

ネストされたクラスを使用するクラスがあり、ネストされたクラスを使用して上位クラスでoperator<<定義したいと考えています。operator<<私のコードは次のようになります。

#include <memory>
#include <iostream>

template<typename T>
struct classA {
  struct classB
  {
    template<typename U>
    friend inline std::ostream& operator<< (std::ostream &out,
                                            const typename classA<U>::classB &b);
  };

  classB root;

  template<typename U>
  friend std::ostream& operator<< (std::ostream &out,
                                   const classA<U> &tree);
};

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const classA<T> &tree)
{
  out << tree.root;
  return out;
}

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const typename classA<T>::classB &b)
{
  return out;
}

int main()
{
  classA<int> a;
  std::cout << a;
}
  • C++11 をサポートせずにコンパイルすると、内部クラスの operator<< の定義がコンパイラによって検出されないようです。

    so.hpp:24:7: error: no match for ‘operator<<’ in ‘out << tree.classA<int>::root’
    so.hpp:24:7: note: candidates are: ...
    
  • std=c++0x でコンパイルする場合の GCC 4.6 および 4.7:

    so.hpp:21:3: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
    In file included from /usr/include/c++/4.7/iostream:40:0,
                     from so.hpp:2:
    /usr/include/c++/4.7/ostream:600:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = classA<int>::classB]’
    

このコードが合法ではない理由と、私が望むことを行うための最良の方法を誰か教えてもらえますか?

4

3 に答える 3

29

この演算子の「推論不可能なコンテキスト」に問題があります

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const typename classA<T>::classB &b)
{
  return out;
}

コンパイラーは、どの値が渡したいパラメーターと一致Tする結果になるかを理解できません。classBしたがって、このテンプレートは考慮されません。

C ++ 11モードでは、コンパイラは標準ライブラリから厳密に一致するものを見つけます。

operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)

ここで、を含むほぼすべてのタイプに一致する可能性がありますが、最初のパラメーターは一致しないことに注意してください。_TpclassA<T>::classB

于 2012-05-18T11:24:14.987 に答える
24

Bo は、これが発生する理由Tを提供しました (入れ子になった への呼び出しでは、型は推定できませんoperator<<。これに対する簡単な回避策であり、ここだけでなく一般的に推奨することは、テンプレートとフレンドになるのではなく、1 つの無料のそのためには、関数をインラインで定義する必要があります。

template<typename T>
struct classA {
  struct classB
  {
    friend inline std::ostream& operator<< (std::ostream &out,
                                            const classB &b) {
       // definition goes here
    }
  };

  classB root;

  friend std::ostream& operator<< (std::ostream &out,
                                   const classA<U> &tree) {
       // definition goes here
  }
};

2 つのアプローチにはいくつかの違いがあります。最も重要なのは、このアプローチでは、テンプレートのインスタンス化ごとに非テンプレート オーバーロードをコンパイラに定義させるoperator<<ことです。テンプレートはテンプレートではないため、引数の推定に依存しません。もう 1 つの副作用は、アプローチが少しきついことです (最初のアプローチでは、テンプレートとすべての可能なインスタンス化 (クラスの内部にアクセスするための抜け穴として使用できます) と親しくなりましたが、1 つの関数だけと親しくなりました)。このように定義された関数は ADL を介してのみ検出されるため、引数がまたはoperator<<でない場合にコンパイラが考慮するオーバーロードが少なくなります。ClassA<T>ClassA<T>::ClassB


アプローチでアクセスを獲得する方法

namespace {
   struct intruder {
       ClassA & ref;
       intruder( ClassA& r ) : ref(r) {}
   };
   template <>
   std::ostream& operator<< <intruder>( std::ostream& _, ClassA<intruder> const& i ) {
       std::cout << i.ref.private_member << std::endl;
       return _;
   }
}

または、テンプレートの特定の専門分野と友達になることもできます。intruderにのみ公開されるoperator<<ためClassA<intruder>、影響ははるかに少ないため、これで問題は解決します。ただし、型はまだ推定できないため、これは特定の問題を解決しません。

于 2012-05-18T11:49:09.723 に答える
2

これを試して:

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                             const classA<T> &tree)
{
   //out << tree.root;
   ::operator<<( out, tree.root);
   return out;
}

そして、あなたは無能の率直な自白を得るでしょう:

test.cpp:34:3: error: no matching function for call to ‘operator<<(std::ostream&, const classA<int>::classB&)’
test.cpp:34:3: note: candidates are:
test.cpp:23:22: note: template<class T> std::ostream& operator<<(std::ostream&, const     typename classA<T>::classB&)
test.cpp:30:22: note: template<class T> std::ostream& operator<<(std::ostream&, const classA<T>&)

回避策: ネストされた classB でメンバー関数を使用して、operator<< の代わりに使用することもできます... もちろん、この解決策には多くの欠点がありますが、この急いでいる必要はありません。

于 2012-05-18T11:32:07.043 に答える