- string[meta header]
- std[meta namespace]
- basic_string[meta class]
- function[meta id-type]
basic_string(); // (1) C++14
explicit basic_string(const Allocator&); // (2) C++14
basic_string() noexcept(noexcept(Allocator())) : basic_string(Allocator()) { } // (1) C++17
explicit basic_string(const Allocator& a) noexcept; // (2) C++17
explicit basic_string(const Allocator& a = Allocator()); // (1) + (2) C++03
basic_string(const basic_string& str); // (3)
basic_string(basic_string&& str) noexcept; // (4) C++11
basic_string(const basic_string& str,
size_type pos,
size_type n = npos,
const Allocator& a = Allocator()); // (5) C++14まで
basic_string(const basic_string& str,
size_type pos,
size_type n,
const Allocator& a = Allocator()); // (5) C++17
basic_string(const basic_string& str,
size_type pos,
const Allocator& a = Allocator()); // (6) C++17
basic_string(const charT* s,
size_type n,
const Allocator& a = Allocator()); // (7)
basic_string(const charT* s,
const Allocator& a = Allocator()); // (8)
basic_string(size_type n,
charT c,
const Allocator& a = Allocator()); // (9)
template <class InputIterator>
basic_string(InputIterator begin, InputIterator end,
const Allocator& a = Allocator()); // (10)
basic_string(initializer_list<charT> init,
const Allocator& = Allocator()); // (11) C++11
basic_string(const basic_string& str, const Allocator&); // (12) C++11
basic_string(basic_string&& str, const Allocator&); // (13) C++11
explicit basic_string(std::basic_string_view<charT, traits> sv,
const Allocator& a = Allocator()); // (14) C++17- initializer_list[link /reference/initializer_list/initializer_list.md]
- (1) : デフォルトコンストラクタ。空の
basic_stringオブジェクトを構築する。 - (2) : アロケータを受け取るデフォルトコンストラクタ。空の
basic_stringオブジェクトを構築する。 - (3) : コピーコンストラクタ。
strオブジェクトと同じ文字列を構築する。 - (4) : ムーブコンストラクタ。
strオブジェクトが指すデータの所有権を自身に移動する。strは未規定の値になる。 - (5) :
strオブジェクトの部分文字列のコピーからbasic_stringオブジェクトを構築する。strオブジェクトのpos番目からn文字の部分文字列がコピーされる。n == nposの場合、pos番目から末尾までの部分文字列がコピーされる。 - (6) :
strオブジェクトの部分文字列のコピーからbasic_stringオブジェクトを構築する。strオブジェクトのpos番目から末尾までの部分文字列がコピーされる。 - (7) : 文字配列
sの先頭n文字からなる部分文字列のコピーからbasic_stringオブジェクトを構築する。 - (8) : 文字配列
sのコピーからbasic_stringオブジェクトを構築する。 - (9) : 文字
cのn回繰り返した文字列からなるbasic_stringオブジェクトを構築する。 - (10) : 文字列の範囲
[begin, end)からbasic_stringオブジェクトを構築する。 - (11) : 文字の初期化子リストから
basic_stringオブジェクトを構築する。 - (12) : アロケータを受け取るコピーコンストラクタ。
- (13) : アロケータを受け取るムーブコンストラクタ。
- (14) :
std::basic_string_viewオブジェクトからの変換コンストラクタ。svが参照する範囲の文字列を*thisにコピーする
- (7)
- C++03 :
sがヌルポインタではないこと。n < nposであること。 - C++14 :
sは、charT型の要素を少なくてもn個を持つ配列を指していること。
- C++03 :
- (8)
- C++03 :
sがヌルポインタではないこと。 - C++14 :
sは、charT型の要素を少なくてもtraits::length(s) + 1個持つ配列を指していること。
- C++03 :
- (5), (6) :
pos > str.size()の場合、out_of_range例外を送出する。 - (13) :
alloc == str.get_allocator()の場合、例外を投げない。
-
C++14 では、
explicit basic_string(const Allocator& a = Allocator())がデフォルト引数を使用しない 2 つのオーバーロードに分割された。
これは、デフォルトコンストラクタにexplicitが付いていると、std::basic_string<char> s = {};のようなコード(C++11 から導入された、コピーリスト初期化によるデフォルトコンストラクタ呼び出し)がエラーになってしまうためである。
#include <iostream>
#include <string>
#include <utility>
int main()
{
// デフォルト構築
std::string s1;
std::cout << "s1 : " << s1 << std::endl;
// 文字配列からの構築
std::string s2 = "hello";
std::cout << "s2 : " << s2 << std::endl;
// コピー構築
std::string s3 = s2;
std::cout << "s3 : " << s3 << std::endl;
// ムーブ構築
std::string s4 = std::move(s3);
std::cout << "s4 : " << s4 << std::endl;
// 部分文字列のコピーから構築
// s4文字列オブジェクトの1番目の文字から3文字
std::string s5(s4, 1, 3);
std::cout << "s5 : " << s5 << std::endl;
// 文字配列の先頭N文字から構築
std::string s6("hello", 3);
std::cout << "s6 : " << s6 << std::endl;
// 文字をN回繰り返して構築
std::string s7(3, 'a');
std::cout << "s7 : " << s7 << std::endl;
// 文字列の範囲から構築
std::string s8(s4.begin(), s4.end());
std::cout << "s8 : " << s8 << std::endl;
// 文字の初期化子リストから構築
std::string s9 = {'h', 'e', 'l', 'l', 'o'};
std::cout << "s9 : " << s9 << std::endl;
// string_viewからの変換
auto sv = std::string_view{"Hello World"}.substr(0, 5);
std::string s14 {sv};
std::cout << "s14 : " << s14 << std::endl;
}- std::move[link /reference/utility/move.md]
- s4.begin()[link begin.md]
- s4.end()[link end.md]
s1 :
s2 : hello
s3 : hello
s4 : hello
s5 : ell
s6 : hel
s7 : aaa
s8 : hello
s9 : hello
s14 : Hello
- N2679 Initializer Lists for Standard Containers(Revision 1)
- (11)の経緯となる提案文書
- LWG Issue 2069. Inconsistent exception spec for
basic_stringmove constructor - LWG Issue 2193. Default constructors for standard library containers are explicit
explicit basic_string(const Allocator& a = Allocator())を 2 つのオーバーロードに分割するきっかけとなったレポート
- LWG Issue 2235. Undefined behavior without proper requirements on
basic_stringconstructors- C++14で、(7)と(8)の要件を見直した。
- LWG Issue 2583. There is no way to supply an allocator for
basic_string(str, pos) - P0254R2 Integrating
std::string_viewandstd::string - N4258 Cleaning-up noexcept in the Library, Rev 3