Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::to_chars_result

      From cppreference.com
      <cpp‎ |utility
       
       
       
      Defined in header<charconv>
      struct to_chars_result;
      (since C++17)

      std::to_chars_result is the return type ofstd::to_chars. It has no base classes, and only has the following members.

      Contents

      [edit]Data members

      Member name Definition
      ptr
      a pointer of typechar*
      (public member object)
      ec
      an error code of typestd::errc
      (public member object)

      [edit]Member and friend functions

      operator==(std::to_chars_result)

      friendbool operator==(const to_chars_result&,
                             const to_chars_result&)=default;
      (since C++20)

      Compares the two arguments usingdefault comparisons (which usesoperator== to compareptr andec respectively).

      This function is not visible to ordinaryunqualified orqualified lookup, and can only be found byargument-dependent lookup when std::to_chars_result is an associated class of the arguments.

      The!= operator issynthesized fromoperator==.

      operator bool

      constexprexplicit operatorbool()constnoexcept;
      (since C++26)

      Checks whether the conversion is successful. Returnsec==std::errc{}.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_to_chars201611L(C++17)Elementary string conversions (std::to_chars,std::from_chars)
      202306L(C++26)Testing for success or failure of<charconv> functions

      [edit]Example

      Run this code
      #include <array>#include <charconv>#include <iostream>#include <string_view>#include <system_error> void show_to_chars(auto...format_args){std::array<char,10> str; #if __cpp_lib_to_chars >= 202306L and __cpp_structured_bindings >= 202406L// use C++26 structured bindings declaration as condition (P0963)// and C++26 to_chars_result::operator bool() for error checking (P2497)if(auto[ptr, ec]=std::to_chars(str.data(), str.data()+ str.size(), format_args...))std::cout<<std::string_view(str.data(), ptr)<<'\n';elsestd::cout<<std::make_error_code(ec).message()<<'\n';#elif __cpp_lib_to_chars >= 202306L// use C++26 to_chars_result::operator bool() for error checking (P2497)if(auto result=std::to_chars(str.data(), str.data()+ str.size(), format_args...))std::cout<<std::string_view(str.data(), result.ptr)<<'\n';elsestd::cout<<std::make_error_code(result.ec).message()<<'\n';#else// fallback to C++17 if-with-initializer and structured bindingsif(auto[ptr, ec]=std::to_chars(str.data(), str.data()+ str.size(), format_args...);        ec==std::errc())std::cout<<std::string_view(str.data(), ptr- str.data())<<'\n';elsestd::cout<<std::make_error_code(ec).message()<<'\n';#endif} int main(){    show_to_chars(42);    show_to_chars(+3.14159F);    show_to_chars(-3.14159, std::chars_format::fixed);    show_to_chars(-3.14159, std::chars_format::scientific,3);    show_to_chars(3.1415926535, std::chars_format::fixed,10);}

      Possible output:

      423.14159-3.14159-3.142e+00Value too large for defined data type

      [edit]See also

      (C++17)
      converts an integer or floating-point value to a character sequence
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/to_chars_result&oldid=179103"

      [8]ページ先頭

      ©2009-2025 Movatter.jp