ライブラリ内のstd::stringのコンテナに汎用の行ベースのIOを提供したいと思います。文字列にはスペースが含まれる場合があるため、行ベース。次のコードは正常に機能しているように見えますが、これが最善の方法であるかどうか、またはそれがいくつかのあいまいさを生み出すかどうかはわかりません。私は理解できていません。
#define boostForeach BOOST_FOREACH
template< template<typename ELEM, typename ALLOC=std::allocator<ELEM> > class Container >
std::ostream& operator<< (std::ostream& o, Container<std::string>const & container){
boostForeach(std::string const& str, container) {
o << str << "\n";
}
return o;
}
template< template<typename ELEM, typename ALLOC=std::allocator<ELEM> > class Container >
std::istream& operator>> (std::istream& in, Container<std::string>& container){
container.clear();
std::string buf;
while(getline(in, buf)) {
if(buf.empty()) break; //stop if empty line found to separate map from other data
container.insert(container.end(),buf);
}
return in;
}
だから問題は:これは安全で健全ですか?