std::string
割り当てられたメモリをから盗むことができる場合、のsubstr
操作は右辺値に対してはるかに効率的である可能性があることに気づきました*this
。
N3225の標準ライブラリには、次のメンバー関数宣言が含まれています。std::string
basic_string substr(size_type pos = 0, size_type n = npos) const;
右辺値用に最適化されたものを実装できる実装は、substr
それをオーバーロードし、2つのバージョンを提供できますか?そのうちの1つは右辺値文字列のバッファーを再利用できますか?
basic_string substr(size_type pos = 0) &&;
basic_string substr(size_type pos, size_type n) const;
*this
設定のメモリを移動元の状態に再利用して、右辺値バージョンを次のように実装できると思います*this
。
basic_string substr(size_type pos = 0) && {
basic_string __r;
__r.__internal_share_buf(pos, __start + pos, __size - pos);
__start = 0; // or whatever the 'empty' state is
return __r;
}
これは一般的な文字列の実装で効率的に機能しますか、それともハウスキーピングが多すぎますか?