GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_string::reserve - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. reserve

最終更新日時: 2026年10月03日 22時31分04秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string>

std::basic_string::reserve

void reserve(size_type res_arg = 0);       // (1)+(2) C++98

void reserve();  // (1) C++20で非推奨化, C++26で削除

constexpr void reserve(size_type res_arg); // (2) C++20

概要

basic_string が最適にメモリを確保できるよう、あらかじめサイズ変更の予定を指示する。

効果

  • C++98 : capacity() >= res_arg となる
  • C++20 : 現在のcapacity()がres_argより小さい場合に限り、capacity() >= res_arg となるようメモリの伸長をリクエストする

戻り値

なし

例外

res_arg > max_size() の場合、length_error 例外を投げる。
allocator_traits<Allocator>::allocate() が、よりふさわしい例外を投げるかもしれない。

非推奨・削除の詳細

C++98でreserve操作でのメモリの縮小ができたことは、以下の問題があった:

  • パフォーマンスの罠となっていた。この関数に指定する引数の値を慎重に選ばなければ、予想外の動的な再確保によってパフォーマンスを低下させる原因となっていた
  • 移植性の壁になっていた。メモリ縮小は実装に任せられたオプション機能であったため、環境による動作の違いがあった
  • vectorとstringでのコードの汎用化がむずかしくなっていた。vector::reserve()はメモリ伸長のみをサポートしていたが、string側はメモリ縮小もサポートしていたため、引数の値を計算することが難しかった
  • メモリ縮小のためにはshrink_to_fit()メンバ関数があるため、そちらと重複する機能であった

これらのことから、C++20では、メモリ縮小を許可しているように見えるデフォルト引数0を非推奨化し、メモリ縮小機能を効果から削除した。

例

#include <iostream>
#include <string>

int main()
{
  std::string s;
  s.reserve(3);

  // 確保したサイズを確認
  std::size_t cap = s.capacity();
  std::cout << cap << std::endl;

  // reserveしたサイズを越えない限り、
  // push_backのたびにメモリの再確保が起こらない
  s.push_back('a');
  s.push_back('b');
  s.push_back('c');
}

出力例

3

参照

  • P0966R1 string::reserve Should Not Shrink
  • P0980R1 Making std::string constexpr
  • P2870R3 Remove basic_string::reserve() From C++26
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.