std:: from_chars_result
|
定義於ヘッダ
<charconv>
|
||
|
struct
from_chars_result
;
|
(C++17以降) | |
std::from_chars_result
は
std::from_chars
の戻り値型です。基底クラスを持たず、以下のメンバーのみを有します。
目次 |
データメンバ
| メンバー名 | 定義 |
|
ptr
|
const
char
*
型のポインタ
(公開メンバーオブジェクト) |
|
ec
|
std::errc
型のエラーコード
(公開メンバーオブジェクト) |
メンバー関数とフレンド関数
operator== (std::from_chars_result)
|
friend
bool
operator
==
(
const
from_chars_result
&
,
const from_chars_result & ) = default ; |
(C++20以降) | |
2つの引数を
デフォルト比較
を使用して比較します(これは
operator
==
を使用してそれぞれ
ptr
と
ec
を比較します)。
この関数は通常の 非修飾名探索 または 修飾名探索 では可視化されず、std::from_chars_resultが引数の関連クラスである場合にのみ 実引数依存探索 によって発見されます。
!=
演算子は
operator==
から合成
されます。
operator bool
|
constexpr
explicit
operator
bool
(
)
const
noexcept
;
|
(C++26以降) | |
変換が成功したかどうかをチェックします。 ec == std:: errc { } を返します。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_to_chars
|
201611L
|
(C++17) | 基本文字列変換 ( std::to_chars , std::from_chars ) |
202306L
|
(C++26) | <charconv> 関数の成功・失敗テスト |
例
#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のconstexpr from_charsデモ / C++26のoperator bool()デモ: 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++17)
|
文字シーケンスを整数値または浮動小数点値に変換する
(関数) |