今日のC++:using指令,例外処理,クラス不変条件
- using指令:
usinng namespace std
的な構文のことをusing指令
というらしい- using宣言とも
- https://ja.cppreference.com/w/cpp/language/using_declaration
- どっちでもいいと思うけど、同じ概念っぽい
- クラス不変条件(invariant)
Vector::Vector(int s) :elem{new double[s]},sz{s} { if(s<0) { throw length_error{}; } elem = new double[s]; sz=s; }
- length_errorの呼び出しがコンパイルできない
No matching constructor for initialization of 'std::length_error' candidate constructor not viable: requires single argument '__s', but no arguments were provided candidate constructor not viable: requires single argument '__s', but no arguments were provided candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
なんかコンストラクタの引数不足っぽい感じだから
throw length_error{""};
としたら動いた。- 本の誤植か?それとも引数を省略できるような俺が知らない設定があるのか?
- length_error にデフォルトコンストラクタが定義されてないから誤植っぽい。
なぜかlength_errorのcatchブロックのコードが実行されない。
- bad_allocのブロックは実行される。(謎) 033
void invariant_test(){ try{ Vector v(-27); } catch(std::length_error){ std::cout << "負数を処理" << std::endl; } catch(std::bad_alloc){ std::cout << "記憶領域不足を処理" << std::endl; } }
経験豊富なCプログラマとC++プログラマは、本章で述べた基盤が、Cや、C++11のサブセットであるC++98とは厳密には対応しないことに注意しよう。
一章終わり
- 作者: ビャーネ・ストラウストラップ,Bjarne Stroustrup,柴田望洋
- 出版社/メーカー: SBクリエイティブ
- 発売日: 2015/02/28
- メディア: 単行本
- この商品を含むブログ (11件) を見る