Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/locale/num get"

From cppreference.com
< cpp‎ | locale
m (Text replace - "{{cpp|" to "{{c|")
(Added LWG issue #2392 DR (part 3).)
 
(22 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{cpp/title|num_get}}
 
{{cpp/title|num_get}}
{{cpp/locale/num_get/sidebar}}
+
{{cpp/locale/num_get/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | locale}}
+
{{dcl header|locale}}
{{ddcl list item | 1=
+
{{dcl|1=
template< class charT,
+
template<  
          class InputIterator = std::istreambuf_iterator<charT>
+
    class CharT,
> class num_get : public std::locale::facet;
+
    class InputIt = std::istreambuf_iterator<CharT>
 +
> class num_get;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Class {{tt|std::num_get}} encapsulates the rules for parsing string representations of values of type {{c|bool}}, {{c|unsigned short}}, {{c|unsigned int}}, {{c|long}}, {{c|unsigned long}}, {{c|long long}}, {{c|unsigned long long}}, {{c|float}}, {{c|double}}, {{c|long double}}, and {{c|void*}}. The standard formatting input operators (such as {{c|cin >> n;}}) use the {{tt|std::num_get}} facet of the I/O stream's locale to parse the text representations of the numbers.
+
Class {{tt|std::num_get}} encapsulates the rules for parsing string representations of numeric values. Specifically, types {{c/core|bool}}, {{c/core|unsigned short}}, {{c/core|unsigned int}}, {{c/core|long}}, {{c/core|unsigned long}}{{rev inl|since=c++11|, {{c/core|long long}}, {{c/core|unsigned long long}}}}, {{c/core|float}}, {{c/core|double}}, {{c/core|long double}}, and {{c/core|void*}} are supported. The standard formatting input operators (such as {{c|cin >> n;}}) use the {{tt|std::num_get}} facet of the I/O stream's locale to parse the text representations of the numbers.
  
Two specializations and two partial specializations are provided by the standard library and are implemented by all locale objects created in a C++ program:
+
{{inheritance diagram/std-num_get}}
  
{{tdcl list begin}}
+
If a {{tt|std::num_get}} specialization is not guaranteed to be provided by the standard library (see below), the behaviors of its {{lc|get()}} and {{lc|do_get()}} are not guaranteed as specified.
{{tdcl list header | locale }}
+
{{tdcl list item | {{c|std::num_get<char>}} | creates narrow string parsing of numbers}}
+
{{tdcl list item | {{c|std::num_get<wchar_t>}} | creates wide string parsing of numbers}}
+
{{tdcl list item | {{c|std::num_get<char, InputIterator>}} | creates narrow string parsing of numbers using custom input iterator}}
+
{{tdcl list item | {{c|std::num_get<wchar_t, InputIterator>}} | creates wide string parsing of numbers using custom input iterator}}
+
{{tdcl list end}}
+
  
===Member types===
+
===Specializations===
{{tdcl list begin}}
+
The standard library is guaranteed to provide the following specializations (they are {{rlp|locale|required to be implemented by any locale object}}):
{{tdcl list hitem | Member type | Definition}}
+
{{dsc begin}}
{{tdcl list item | {{tt|char_type}} | {{tt|charT}}}}
+
{{dsc header|locale}}
{{tdcl list item | {{tt|iter_type}} | {{tt|InputIterator}}}}
+
{{dsc|{{c/core|std::num_get<char>}}|creates narrow string parsing of numbers}}
{{tdcl list end}}
+
{{dsc|{{c/core|std::num_get<wchar_t>}}|creates wide string parsing of numbers}}
 +
{{dsc end}}
  
===Member objects===
+
In addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:
{{tdcl list begin}}
+
* {{tt|CharT}} is one of
{{tdcl list hitem | Member name | Type}}
+
** {{c/core|char}},
{{tdcl list item | {{tt|id}} {{mark|static}} | {{c|std::locale::id}} }}
+
** {{c/core|wchar_t}}, and
{{tdcl list end}}
+
** any other implementation-defined [[cpp/string|character container type]] that meets the requirements for a character on which any of the [[cpp/io|iostream components]] can be instantiated; and
 +
* {{tt|InputIt}} must meet the requirements of {{named req|InputIterator}}.
 +
 
 +
===Member types===
 +
{{dsc begin}}
 +
{{dsc hitem|Member type|Definition}}
 +
{{dsc|{{tt|char_type}}|{{tt|CharT}}}}
 +
{{dsc|{{tt|iter_type}}|{{tt|InputIt}}}}
 +
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/locale/num_get/num_get | constructs a new num_get facet }}
+
{{dsc mem ctor|cpp/locale/num_get/num_get|constructs a new {{tt|num_get}} facet}}
{{dcl list prot mem dtor | cpp/locale/num_get/~num_get | destructs a num_get facet }}
+
{{dsc mem fun|cpp/locale/num_get/get|invokes {{tt|do_get}}}}
{{dcl list mem fun | cpp/locale/num_get/do_get | title=get | invokes {{tt|do_get}} }}
+
{{dsc end}}
{{dcl list end}}
+
 
 +
===Member objects===
 +
{{dsc begin}}
 +
{{dsc mem obj|nolink=true|{{dsc small|static std::locale::id}} id|''id'' of the locale}}
 +
{{dsc end}}
  
 
===Protected member functions===
 
===Protected member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list prot mem vfun | cpp/locale/num_get/do_get | parses a number from an input stream}}
+
{{dsc prot mem dtor|cpp/locale/num_get/~num_get|destructs a {{tt|num_get}} facet}}
{{dcl list end}}
+
{{dsc prot mem vfun|cpp/locale/num_get/get|title=do_get|parses a number from an input stream}}
 +
{{dsc end}}
  
 
===Example===
 
===Example===
 
 
{{example
 
{{example
|
+
|code=
| code=
+
 
#include <iostream>
 
#include <iostream>
 +
#include <iterator>
 
#include <locale>
 
#include <locale>
#include <string>
 
 
#include <sstream>
 
#include <sstream>
#include <iterator>
+
#include <string>
 +
 
 
int main()
 
int main()
 
{
 
{
 
     std::string de_double = "1.234.567,89";
 
     std::string de_double = "1.234.567,89";
 
     std::string us_double = "1,234,567.89";
 
     std::string us_double = "1,234,567.89";
 
+
   
 
     // parse using streams
 
     // parse using streams
 
     std::istringstream de_in(de_double);
 
     std::istringstream de_in(de_double);
     de_in.imbue(std::locale("de_DE"));
+
     de_in.imbue(std::locale("de_DE.UTF-8"));
 
     double f1;
 
     double f1;
 
     de_in >> f1;
 
     de_in >> f1;
 
+
   
 
     std::istringstream us_in(de_double);
 
     std::istringstream us_in(de_double);
 
     us_in.imbue(std::locale("en_US.UTF-8"));
 
     us_in.imbue(std::locale("en_US.UTF-8"));
 
     double f2;
 
     double f2;
 
     us_in >> f2;
 
     us_in >> f2;
 
+
   
 
     std::cout << "Parsing " << de_double << " as double gives " << std::fixed
 
     std::cout << "Parsing " << de_double << " as double gives " << std::fixed
 
               << f1 << " in de_DE locale and " << f2 << " in en_US\n";
 
               << f1 << " in de_DE locale and " << f2 << " in en_US\n";
 
+
   
 
     // use the facet directly
 
     // use the facet directly
 
     std::istringstream s3(us_double);
 
     std::istringstream s3(us_double);
 
     s3.imbue(std::locale("en_US.UTF-8"));
 
     s3.imbue(std::locale("en_US.UTF-8"));
 +
   
 
     auto& f = std::use_facet<std::num_get<char>>(s3.getloc());
 
     auto& f = std::use_facet<std::num_get<char>>(s3.getloc());
 
     std::istreambuf_iterator<char> beg(s3), end;
 
     std::istreambuf_iterator<char> beg(s3), end;
Line 84: Line 93:
 
     std::ios::iostate err;
 
     std::ios::iostate err;
 
     f.get(beg, end, s3, err, f3);
 
     f.get(beg, end, s3, err, f3);
 +
   
 
     std::cout << "parsing " << us_double
 
     std::cout << "parsing " << us_double
 
               << " as double using raw en_US facet gives " << f3 << '\n';
 
               << " as double using raw en_US facet gives " << f3 << '\n';
 
}
 
}
| output=
+
|output=
 
Parsing 1.234.567,89 as double gives 1234567.890000 in de_DE locale and 1.234000 in en_US
 
Parsing 1.234.567,89 as double gives 1234567.890000 in de_DE locale and 1.234000 in en_US
 
parsing 1,234,567.89 as double using raw en_US facet gives 1234567.890000
 
parsing 1,234,567.89 as double using raw en_US facet gives 1234567.890000
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=427|std=C++98|before={{tt|num_get}} was guaranteed to accept any {{tt|CharT}} that<br>meets the requirements for a character on which<br>any of the iostream components can be instantiated|after=only guarantees to accept {{c/core|char}},<br>{{c/core|wchar_t}} and other implementation-<br>defined character types}}
 +
{{dr list item|wg=lwg|dr=2392|std=C++98|before=only character type {{tt|CharT}} could be<br>guaranteed to be accepted by {{tt|num_get}}|after=can guarantee to accept implementation-<br>defined character container types}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/locale/dcl list numpunct}}
+
{{dsc inc|cpp/locale/dsc numpunct}}
{{dcl list template | cpp/locale/dcl list num_put}}
+
{{dsc inc|cpp/locale/dsc num_put}}
{{dcl list template | cpp/io/basic_istream/dcl list operator_gtgt}}
+
{{dsc inc|cpp/io/basic_istream/dsc operator_gtgt}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 22:32, 1 February 2024

 
 
 
 
Defined in header <locale>
template<

    class CharT,
    class InputIt = std::istreambuf_iterator<CharT>

> class num_get;

Class std::num_get encapsulates the rules for parsing string representations of numeric values. Specifically, types bool, unsigned short, unsigned int, long, unsigned long, long long, unsigned long long(since C++11), float, double, long double, and void* are supported. The standard formatting input operators (such as cin >> n;) use the std::num_get facet of the I/O stream's locale to parse the text representations of the numbers.

cpp/locale/locale/facetstd-num get-inheritance.svg

Inheritance diagram

If a std::num_get specialization is not guaranteed to be provided by the standard library (see below), the behaviors of its get() and do_get() are not guaranteed as specified.

Contents

[edit] Specializations

The standard library is guaranteed to provide the following specializations (they are required to be implemented by any locale object):

Defined in header <locale>
std::num_get<char> creates narrow string parsing of numbers
std::num_get<wchar_t> creates wide string parsing of numbers

In addition, the standard library is also guaranteed to provide every specialization that satisfies the following type requirements:

[edit] Member types

Member type Definition
char_type CharT
iter_type InputIt

[edit] Member functions

constructs a new num_get facet
(public member function)
invokes do_get
(public member function)

[edit] Member objects

static std::locale::id id
id of the locale
(public member object)

[edit] Protected member functions

destructs a num_get facet
(protected member function)
[virtual]
parses a number from an input stream
(virtual protected member function)

[edit] Example

#include <iostream>
#include <iterator>
#include <locale>
#include <sstream>
#include <string>
 
int main()
{
    std::string de_double = "1.234.567,89";
    std::string us_double = "1,234,567.89";
 
    // parse using streams
    std::istringstream de_in(de_double);
    de_in.imbue(std::locale("de_DE.UTF-8"));
    double f1;
    de_in >> f1;
 
    std::istringstream us_in(de_double);
    us_in.imbue(std::locale("en_US.UTF-8"));
    double f2;
    us_in >> f2;
 
    std::cout << "Parsing " << de_double << " as double gives " << std::fixed
              << f1 << " in de_DE locale and " << f2 << " in en_US\n";
 
    // use the facet directly
    std::istringstream s3(us_double);
    s3.imbue(std::locale("en_US.UTF-8"));
 
    auto& f = std::use_facet<std::num_get<char>>(s3.getloc());
    std::istreambuf_iterator<char> beg(s3), end;
    double f3;
    std::ios::iostate err;
    f.get(beg, end, s3, err, f3);
 
    std::cout << "parsing " << us_double
              << " as double using raw en_US facet gives " << f3 << '\n';
}

Output:

Parsing 1.234.567,89 as double gives 1234567.890000 in de_DE locale and 1.234000 in en_US
parsing 1,234,567.89 as double using raw en_US facet gives 1234567.890000

[edit] Defect reports

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

DR Applied to Behavior as published Correct behavior
LWG 427 C++98 num_get was guaranteed to accept any CharT that
meets the requirements for a character on which
any of the iostream components can be instantiated
only guarantees to accept char,
wchar_t and other implementation-
defined character types
LWG 2392 C++98 only character type CharT could be
guaranteed to be accepted by num_get
can guarantee to accept implementation-
defined character container types

[edit] See also

defines numeric punctuation rules
(class template) [edit]
formats numeric values for output as character sequence
(class template) [edit]
extracts formatted data
(public member function of std::basic_istream<CharT,Traits>) [edit]