これは完璧な解決策ではありません(ただし、現時点では、iostreamライブラリを読まないと別の方法を考えることはできません)。
マニピュレータは次のとおりです。
class MaxFieldSize {/*STUFF*/};
ストリーム演算子を作成するときは、実際のストリームを返さない(むしろ、ラッパーを含むストリームを返す)少しファンキーな演算子を作成します。
MaxFieldWdithStream operator<<(std::ostream&, MaxFieldSize const& manip);
ここで、このクラスのすべてのストリーム演算子をオーバーロードして、通常のストリームオブジェクトを返す前に入力を切り捨てます。
class MaxFieldWithStream { std::ostream& printTruncatedData(std::string& value);};
次に、必要なのは一般的なオーバーロードだけです。
template<typename T>
std::ostream& operator<<(MaxFieldWithStream& mfwstream, T const& value)
{
std::stringstream trunStream;
trunStream << value;
return mfwstream.printTruncatedData(trunStream.substr(0, mfwstream.widthNeeded));
}
// You will probably need another overload for io-manipulators.
また、MaxFieldWithStreamをstd :: iostreamに自動的に変換する変換演算子を追加して、関数に渡された場合でもストリームのように動作するようにします(ただし、max widthプロパティは失われます)。
class MaxFieldWithStream
{
std::ostream& printTruncatedData(std::string& value);};
operator st::ostream&() const { return BLABLAVLA;}
};