![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
うわ。Boost.Optional 終わりすぎ。 optional<T&> って T* と同じもんじゃん。なんでサイズ違うんだ>< http://ideone.com/PIEfo
2010-06-03 04:20:36![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
sizeof( boost::optional<T&> ) が sizeof( T* ) と一致しない件について、ブログに文句を追記した。これは本気で爆発すべき動作。
2010-06-03 04:30:28![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
@YurineMashiro T& に関しては、内部状態として T* を持てばいいので、同じサイズで実現可能と言うか、実現して欲しかったです。
2010-06-03 04:43:11![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
@SubaruG なるほど、確かにそうですね。そうなるとT&がoptionalに入れられちゃうのもちょっと気持ち悪いような気もします。
2010-06-03 04:45:01![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
言うまでもないことだけど、 boost::optional<T&> は、代入の構文以外は生ポインタそのものなのですよ。
2010-06-03 04:45:13![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
@YurineMashiro 是非はともかく、optional<T&> は、再設定可能で NULL にもなれる参照、という、ポインタよりも狭い概念を示すのに便利なので、僕は割と積極的に使ってました。
2010-06-03 04:47:18![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
所謂 ”rebind" をすることができる参照型ってのは、 T* と reference_wrapper<T> と optional<T&> が代表的ですが
2010-06-03 04:49:07![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
T* は歴史的に参照以外の役割(最たる例はランダムアクセスイテレータ)も持っている、 reference_wrapper は無効参照を取れない、という弱点が有るので、 optional<T&> はかなり有効な選択肢。
2010-06-03 04:50:05![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
というか、意図の把握しづらい生ポインタを駆逐するために、あえて生ポインタと殆ど同じ構文で使える生ポインタの代替として作られたって言う側面もある、と僕は勝手に推測してるわけで。
2010-06-03 04:52:24![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
そうそう、直接は関係ないけど、 boost::optional<T> は、一般の T に対して「 "rebind" できる T 」という側面も有ったりするんですよ。
2010-06-03 04:56:04![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
つまり、代入演算に頼らず、コンストラクタによって中身を書き換えられる、ということ。 optional<T const> は、初期化と "rebind" 以外の一切の変更を禁止するクラスとして有効。
2010-06-03 04:57:00![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
かつ、 "rebind" できるクラス、というものを、例外安全にジェネリックに書いた場合、 Boost.Optional と同じものが出来上がります。
2010-06-03 04:57:27![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
コンストラクタで例外の投げられる可能性が有るクラスを、準手動でメモリ管理する場合、殆どが Boost.Optional に帰結します。
2010-06-03 05:01:17![](https://tgfile.tg-static.com/static/web/img/placeholder.gif)
スタック上に置けるオブジェクトであれば、正になんでもおk。デストラクタで例外を投げると未定義だってのは常識だし。
2010-06-03 05:05:36