標準(N3337
)は(27.5.3.1.1 Class ios_base::failure
)言う:C++のstd :: ios_base ::故障例外
は私がのstd :: ostringstreamの使用中に制限されたリソース環境をエミュレートし、簡単なテストプログラムを持っています。 One of online compilersは同じ出力を示します。クラス障害は入出力ストリームの関数により、例外としてスローされるすべてのオブジェクト のタイプの基本クラスを定義しますストリームバッファ操作中に エラーが検出されたことを報告します。私は
stderr
にCaught: std::bad_alloc
を得た私の環境(Linuxでは、GCC 5.3.0)では#include <sys/time.h> #include <sys/resource.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <sstream> int main(int argc, const char* argv[]) { rlimit limit; limit.rlim_cur = limit.rlim_max = 268435456; if(setrlimit(RLIMIT_AS, &limit)) { std::cerr << "Cannot set resource limit: " << strerror(errno) << std::endl; exit(EXIT_FAILURE); } std::ostringstream os; os.exceptions(std::ostringstream::badbit); try { auto iterations = 1024 * 1024 * 1024; while(iterations && --iterations) os << 'F'; } catch(const std::ios_base::failure& ex) { std::cerr << "Caught: std::ios_base::failure" << std::endl; } catch(const std::bad_alloc& ex) { std::cerr << "Caught: std::bad_alloc" << std::endl; } catch(...) { std::cerr << "Caught: ellipsis" << std::endl; } return 0; }
:
質問は例外タイプがstd::bad_alloc
で、std::ios_base::failure
ではないのはなぜですか?
(バッファ容量は、最初の
bad_alloc
を押すことなくstring::max_size
に達した場合にlibstdC++では、stringbuf::overflow
はまだ、eof
を返すことができますか)? –ios_base :: failure例外のstd :: packに再キャプチャしてパックしませんか?私はメモリアロケータではなくiostreamで作業しています。 – user1641854
あなたはそう思うかもしれませんが、標準はそれを必要としません。また、 'bad_alloc'が発生した後、別の例外を生成するためのスペースが残っていない可能性があるという問題もあります。 –