Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::num_get<CharT,InputIt>::get,std::num_get<CharT,InputIt>::do_get

      From cppreference.com
      <cpp‎ |locale‎ |num get
       
       
       
      Localization library
       
      std::num_get
      Member functions
      num_get::getnum_get::do_get
       
      (1)
      public:

      iter_type get( iter_type in, iter_type end,std::ios_base& str,

                     std::ios_base::iostate& err,bool& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,long& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,longlong& v)const;
      (since C++11)
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,unsignedshort& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,unsignedint& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,unsignedlong& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,unsignedlonglong& v)const;
      (since C++11)
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,float& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,double& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,longdouble& v)const;
      iter_type get( iter_type in, iter_type end,std::ios_base& str,
                     std::ios_base::iostate& err,void*& v)const;
      (2)
      protected:

      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,

                               std::ios_base::iostate& err,bool& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,long& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,longlong& v)const;
      (since C++11)
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,unsignedshort& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,unsignedint& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,unsignedlong& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,

                               std::ios_base::iostate& err,

                               unsignedlonglong& v)const;
      (since C++11)
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,float& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,double& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,longdouble& v)const;
      virtual iter_type do_get( iter_type in, iter_type end,std::ios_base& str,
                               std::ios_base::iostate& err,void*& v)const;
      1) Public member function, calls the member functiondo_get of the most derived class.
      2) Reads characters from the input iteratorin and generates the value of the type ofv, taking into account I/O stream formatting flags fromstr.flags(), character classification rules fromstd::use_facet<std::ctype<CharT>>(str.getloc()), and numeric punctuation characters fromstd::use_facet<std::numpunct<CharT>>(str.getloc()). This function is called by all formatted input stream operators such asstd::cin>> n;.

      Conversion occurs in three stages:

      Contents

      [edit]Stage 1: conversion specifier selection

      • I/O format flags are obtained, as if by
      fmtflags basefield=(str.flags()&std::ios_base::basefield);
      fmtflags boolalpha=(str.flags()&std::ios_base::boolalpha);
      • If the type ofv is an integer type, the first applicable choice of the following five is selected:
      Ifbasefield== oct, will use conversion specifier%o
      Ifbasefield== hex, will use conversion specifier%X
      Ifbasefield==0, will use conversion specifier%i
      If the type ofv is signed, will use conversion specifier%d
      If the type ofv is unsigned, will use conversion specifier%u
      • For integer types, length modifier is added to the conversion specification if necessary:h forshort andunsignedshort,l forlong andunsignedlong,ll forlonglong andunsignedlonglong(since C++11)
      • If the type ofv isfloat, will use conversion specifier%g
      • If the type ofv isdouble, will use conversion specifier%lg
      • If the type ofv islongdouble, will use conversion specifier%Lg
      • If the type ofv isvoid*, will use conversion specifier%p
      • If the type ofv isbool andboolalpha==0, proceeds as if the type ofv islong, except for the value to be stored inv in stage 3.
      • If the type ofv isbool andboolalpha!=0, the following replaces stages 2 and 3:
        • Successive characters obtained from the input iteratorin are matched against the character sequences obtained fromstd::use_facet<std::numpunct<CharT>>(str.getloc()).falsename() andstd::use_facet<std::numpunct<CharT>>(str.getloc()).truename() only as necessary as to identify the unique match. The input iteratorin is compared toend only when necessary to obtain a character.
        • If the target sequence is uniquely matched,v is set to the correspondingbool value. Otherwisefalse is stored inv andstd::ios_base::failbit is assigned toerr. If unique match could not be found before the input ended (in== end),err|=std::ios_base::eofbit is executed.

      [edit]Stage 2: character extraction

      • Ifin== end, stage 2 is terminated immediately, no further characters are extracted.
      • The next character is extracted fromin as if bychar_type ct=*in;:
        • If the character matches one of"0123456789abcdefxABCDEFX+-"(until C++11)"0123456789abcdefpxABCDEFPX+-"(since C++11), widened to the locale's char_type as if bystd::use_facet<std::ctype<CharT>>(str.getloc()).widen(), it is converted to the correspondingchar.
        • If the character matches the decimal point separator (std::use_facet<std::numpunct<CharT>>(str.getloc()).decimal_point())), it is replaced by'.'.
        • If the character matches the thousands separator (std::use_facet<std::numpunct<CharT>>(str.getloc()).thousands_sep()) and the thousands separation is in use (as determined bystd::use_facet<std::numpunct<CharT>>(str.getloc()).grouping().length()!=0), then if the decimal point'.' has not yet been accumulated, the position of the character is remembered, but the character is otherwise ignored. If the decimal point has already been accumulated, the character is discarded and stage 2 terminates.
        • In any case, the check is made whether thechar obtained from the previous steps is allowed in the input field that would be parsed bystd::scanf given the conversion specifier selected in stage 1. If it is allowed, it is accumulated in a temporary buffer and stage 2 repeats. If it is not allowed, stage 2 terminates.

      [edit]Stage 3: conversion and storage

      • The sequence ofchars accumulated in stage 2 is converted to a numeric value:
      The input is parsed according to the rules ofstd::scanf.
      (until C++11)
      The input is parsed as if by
      (since C++11)
      • If the conversion function fails to convert the entire field, the value0 is stored inv.
      • If the type ofv is a signed integer type and the conversion function results in a positive or negative value too large to fit in it, the most positive or negative representable value is stored inv, respectively.
      • If the type ofv is an unsigned integer type and the conversion function results in a value that does not fit in it, the most positive representable value is stored inv.
      • In any case, if the conversion function failsstd::ios_base::failbit is assigned toerr.
      • Otherwise, the numeric result of the conversion is stored inv.
        • If the type ofv isbool and boolalpha is not set, then if the value to be stored is0,false is stored, if the value to be stored is1,true is stored, for any other valuestd::ios_base::failbit is assigned toerr andtrue is stored.
      • After this, digit grouping is checked. if the position of any of the thousands separators discarded in stage 2 does not match the grouping provided bystd::use_facet<std::numpunct<CharT>>(str.getloc()).grouping(),std::ios_base::failbit is assigned toerr.
      • If stage 2 was terminated by the testin== end,err|=std::ios_base::eofbit is executed to set the eof bit.

      [edit]Return value

      in

      [edit]Notes

      Before the resolutions ofLWG issue 23 andLWG issue 696,v was left unchanged if an error occurs.

      Before the resolution ofLWG issue 221, strings representing hexadecimal integers (e.g."0xA0") were rejected bydo_get(int) even if they are valid input tostrtol because stage 2 filters out characters'X' and'x'.

      Before the resolution ofLWG issue 1169, converting a negative number string into an unsigned integer might produce zero (since the value represented by the string is smaller than what the target type can represent).

      Before the resolution ofLWG issue 2381, strings representing hexadecimal floating-point numbers with exponents (e.g."0x1.23p-10") were rejected bydo_get(double) even if they are valid input tostrtod because stage 2 filters out characters'P' and'p'.

      The strings representing infinity or not-a-number (e.g."NaN" and"inf") are rejected bydo_get(double) even if they are valid input tostrtod because stage 2 filters out characters such as'N' or'i'.

      (since C++11)

      [edit]Example

      An implementation ofoperator>> for a user-defined type.

      Run this code
      #include <iostream>#include <iterator>#include <locale> struct base{long x;}; template<class CharT,class Traits>std::basic_istream<CharT, Traits>&    operator>>(std::basic_istream<CharT, Traits>& is, base& b){std::ios_base::iostate err=std::ios_base::goodbit; try// setting err could throw{typenamestd::basic_istream<CharT, Traits>::sentry s(is); if(s)// if stream is ready for inputstd::use_facet<std::num_get<CharT>>(is.getloc()).get(is,{}, is, err, b.x);}catch(std::ios_base::failure& error){// handle the exception} return is;} int main(){    base b;std::cin>> b;}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 17C++98the process of parsing text boolean values was errornouscorrected
      LWG 18C++98the overload ofget takingbool& value was missingadded
      LWG 23C++98overflowing input resulted in undefined behavioroverflow handled
      LWG 154C++98the conversion specifier fordouble was%g (same asfloat)changed to%lg
      LWG 221C++98do_get did not parse'x' and'X' whilestrtol parsed themmade'x' and'X' parsed
      LWG 275C++98get had an overload takingshort& value instead offloat&corrected
      LWG 358C++98thousand separators after the decimal point were ignoredstage 2 is terminated if encountered
      LWG 696C++98the result was unchanged on conversion failureset to zero
      LWG 1169C++98overflow handling was inconsistent between floating-point typesmade consistent
      withstrtof/strtod
      LWG 2381C++11do_get did not parse'p' and'P' whilestrtod parsed themmade'p' and'P' parsed

      [edit]See also

      extracts formatted data
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/locale/num_get/get&oldid=160184"

      [8]ページ先頭

      ©2009-2025 Movatter.jp