あなたの質問を読み直した後(この回答のコメントの結果として)、あなたが望むのは文字列への変換(ここの他の回答の私の仮定)だけでなく、内部のストリームへの転送であることに気付きました。
さて、達成したいことは単純ではなく、ほとんどの場合やり過ぎかもしれません。[make_string][3]
私が持っている(内部に転送する)実装ではostringstream
、マニピュレータを渡すことを許可していません。ユーザーが新しい行を追加したい場合 (Linux で開発しています)、'\n' 文字を渡すだけです。
あなたの問題は、マニピュレータ(std::hex
、std::endl
...)の転送です。あなたの operator<< は、型 T の定数インスタンスを取るように定義されていますが、マニピュレーターは関数ポインターであり、コンパイラーはそれをメソッドと照合することができません。
std::basic_ostream
マニピュレータは、テンプレートを操作する関数です。basic_ostream
テンプレートとクラスは次のostream
ように定義されています。
template <typename TChar, typename TTraits = char_traits<TChar> >
class basic_ostream;
typedef basic_ostream<char> ostream;
// or
// typedef basic_ostream<wchar_t> if using wide characters
次に、std::ostream に渡すことができる可能なマニピュレータは次のとおりです。
typedef std::ostream& (*manip1)( std::ostream& );
typedef std::basic_ios< std::ostream::char_type, std::ostream::traits_type > ios_type;
typedef ios_type& (*manip2)( ios_type& );
typedef std::ios_base& (*manip3)( std::ios_base& );
マニピュレータを受け入れたい場合は、クラスでそのオーバーロードを提供する必要があります。
class mystream
{
//...
public:
template <typename T>
mystream& operator<<( T datum ) {
stream << datum;
return *this
}
// overload for manipulators
mystream& operator<<( manip1 fp ) {
stream << fp;
return *this;
}
mystream& operator<<( manip2 fp ) {
stream << fp;
return *this;
}
mystream& operator<<( manip3 fp ) {
stream << fp;
return *this;
}
};
特に、endl の署名 (必要な唯一の署名である可能性があります) は次のとおりです。
template <typename Char, typename Traits>
std::basic_ostream<Char,Traits>&
std::endl( std::basic_ostream<Char,Traits>& stream );
manip1
したがって、関数のタイプに該当します。その他、std::hex
さまざまなカテゴリに分類されるなど (manip3
この特定のケースでは)