std::from_chars
| ヘッダー <charconv> で定義 |
||
| std::from_chars_result from_chars( const char* first, const char* last, |
(1) | (C++17以降) (C++23 以降 constexpr) |
| std::from_chars_result from_chars( const char* first, const char* last, |
(2) | (C++17以降) |
以下に説明するパターンについて、文字シーケンス [first, last) を解析します。パターンに一致する文字がない場合、または一致した文字を解析して得られた値が value の型で表現できない場合、value は変更されません。そうでない場合、パターンに一致する文字は算術値のテキスト表現として解釈され、value に格納されます。
- base が 16 の場合、"0x" または "0X" のプレフィックスは認識されません。
- マイナス記号のみが認識され(プラス記号は認識されない)、value の符号付き整数型の場合のみです。
- 先行する空白は無視されません。
- 指数部の外側ではプラス記号は認識されません(先頭にマイナス記号のみが許可されます)。
fmtに std::chars_format::scientific が設定され、std::chars_format::fixed が設定されていない場合、指数部は必須です(そうでない場合はオプション)。fmtに std::chars_format::fixed が設定され、std::chars_format::scientific が設定されていない場合、オプションの指数部は許可されません。fmtが std::chars_format::hex の場合、プレフィックス "0x" または "0X" は許可されません(文字列 "0x123" は、未解析の残りの部分 "x123" を持つ値 "0" として解析されます)。- 先行する空白は無視されません。
目次 |
[編集] パラメータ
| first, last | - | 解析する有効な文字範囲 |
| value | - | 成功した場合に解析された値が格納される出力パラメータ |
| base | - | 使用する整数基数: 2 から 36 (含む) の値。 |
| fmt | - | 使用する浮動小数点書式設定、std::chars_format 型のビットマスク |
[編集] 戻り値
成功した場合、std::from_chars_result 型の値を返します。この値では、ptr はパターンに一致しない最初の文字を指すか、すべての文字が一致し、ec が値初期化されている場合は last と等しい値を持っています。
パターンに一致しない場合、std::from_chars_result 型の値を返します。この値では、ptr は first と等しく、ec は std::errc::invalid_argument と等しくなります。value は変更されません。
パターンが一致したが、解析された値が value の型で表現可能な範囲にない場合、std::from_chars_result 型の値を返します。この値では、ec は std::errc::result_out_of_range と等しく、ptr はパターンに一致しない最初の文字を指します。value は変更されません。
[編集] 例外
何もスローしません。
[編集] 備考
C++およびCライブラリの他の解析関数とは異なり、std::from_chars はロケールに依存せず、メモリ割り当てを行わず、例外を投げません。他のライブラリ(std::sscanf など)で使用される解析ポリシーのごく一部のみが提供されます。これは、テキストベースの交換(JSONやXML)などの一般的な高スループットのコンテキストで役立つ、可能な限り最速の実装を可能にすることを意図しています。
std::to_chars でフォーマットされたすべての浮動小数点値を std::from_chars が正確に復元できるという保証は、両方の関数が同じ実装のものである場合にのみ提供されます。
符号のみでその後に数字がないパターンは、何も一致しないパターンとして扱われます。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_to_chars |
201611L |
(C++17) | 基本的な文字列変換(std::from_chars, std::to_chars) |
202306L |
(C++26) | <charconv> 関数の成功または失敗のテスト | |
__cpp_lib_constexpr_charconv |
202207L |
(C++23) | 整数型に対する std::from_chars および std::to_chars オーバーロードに constexpr 修飾子を追加 |
[編集] 例
#include <cassert> #include <charconv> #include <iomanip> #include <iostream> #include <optional> #include <string_view> #include <system_error> int main() { for (std::string_view const str : {"1234", "15 foo", "bar", " 42", "5000000000"}) { std::cout << "String: " << std::quoted(str) << ". "; int result{}; auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result); if (ec == std::errc()) std::cout << "Result: " << result << ", ptr -> " << std::quoted(ptr) << '\n'; else if (ec == std::errc::invalid_argument) std::cout << "This is not a number.\n"; else if (ec == std::errc::result_out_of_range) std::cout << "This number is larger than an int.\n"; } // C++23's constexpr from_char demo / C++26's operator bool() demo: auto to_int = [](std::string_view s) -> std::optional<int> { int value{}; #if __cpp_lib_to_chars >= 202306L if (std::from_chars(s.data(), s.data() + s.size(), value)) #else if (std::from_chars(s.data(), s.data() + s.size(), value).ec == std::errc{}) #endif return value; else return std::nullopt; }; assert(to_int("42") == 42); assert(to_int("foo") == std::nullopt); #if __cpp_lib_constexpr_charconv and __cpp_lib_optional >= 202106 static_assert(to_int("42") == 42); static_assert(to_int("foo") == std::nullopt); #endif }
出力
String: "1234". Result: 1234, ptr -> "" String: "15 foo". Result: 15, ptr -> " foo" String: "bar". This is not a number. String: " 42". This is not a number. String: "5000000000". This number is larger than an int.
[編集] 欠陥報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2955 | C++17 | この関数は <utility> にあり、std::error_code を使用していました。 | <charconv> に移動し、std::errc を使用します。 |
| LWG 3373 | C++17 | std::from_chars_result には追加のメンバーがある可能性があります。 |
追加のメンバーは禁止されています。 |
[編集] 関連項目
| (C++17) |
std::from_chars の戻り値の型 (クラス) |
| (C++17) |
整数値または浮動小数点数値を文字シーケンスに変換する (関数) |
| (C++11)(C++11)(C++11) |
文字列を符号付き整数に変換する (function) |
| (C++11)(C++11)(C++11) |
文字列を浮動小数点値に変換する (function) |
| (C++11) |
バイト文字列を整数値に変換する (関数) |
| バイト文字列を浮動小数点値に変換する (関数) | |
| stdin、ファイルストリーム、またはバッファから書式付き入力を読み取ります。 (関数) | |
| 書式付きデータを抽出する ( std::basic_istream<CharT,Traits>のpublicメンバ関数) |