std::num_put<CharT,OutputIt>::put, std::num_put<CharT,OutputIt>::do_put - cppreference.com (original) (raw)

| Defined in header | | | | --------------------------------------------------------------------------------------------------------------------------------------------- | | ------------- | | (1) | | | | public: iter_type put( iter_type out, std::ios_base& str, char_type fill, bool val ) const; | | | | iter_type put( iter_type out, std::ios_base& str, char_type fill, long val ) const; | | | | iter_type put( iter_type out, std::ios_base& str, char_type fill, long long val ) const; | | (since C++11) | | iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long val ) const; | | | | iter_type put( iter_type out, std::ios_base& str, char_type fill, unsigned long long val ) const; | | (since C++11) | | iter_type put( iter_type out, std::ios_base& str, char_type fill, double val ) const; | | | | iter_type put( iter_type out, std::ios_base& str, char_type fill, long double val ) const; | | | | iter_type put( iter_type out, std::ios_base& str, char_type fill, const void* val ) const; | | | | (2) | | | | protected: virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, bool val ) const; | | | | virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long val ) const; | | | | virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long long val ) const; | | (since C++11) | | virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long val ) const; | | | | virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, unsigned long long val ) const; | | (since C++11) | | virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, double val ) const; | | | | virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, long double val ) const; | | | | virtual iter_type do_put( iter_type out, std::ios_base& str, char_type fill, const void* val ) const; | | |

  1. Public member function, calls the protected virtual member function do_put of the most derived class.

  2. Writes characters to the output sequence out which represent the value of val, formatted as requested by the formatting flags str.flags() and the std::numpunct and std::ctype facets of the locale imbued in the stream str. This function is called by all formatted output stream operators, such as std::cout << n;.

Conversion occurs in four stages:

Contents

[edit] Stage 1: conversion specifier selection

fmtflags basefield = (str.flags() & std::ios_base::basefield);

fmtflags uppercase = (str.flags() & std::ios_base::uppercase);

fmtflags floatfield = (str.flags() & std::ios_base::floatfield);

fmtflags showpos = (str.flags() & std::ios_base::showpos);

fmtflags showbase = (str.flags() & std::ios_base::showbase);

fmtflags showpoint = (str.flags() & std::ios_base::showpoint);

If floatfield == (std::ios_base::fixed | std::ios_base::scientific) && !uppercase, will use conversion specifier %a. If floatfield == (std::ios_base::fixed std::ios_base::scientific), will use conversion specifier %A. (since C++11)

Also:

[edit] Stage 2: locale-specific conversion

[edit] Stage 3: padding

In any case, str.width(0) is called to cancel the effects of std::setw.

[edit] Stage 4: output

Every successive character c from the sequence of CharT's from Stage 3 is output as if by *out++ = c.

[edit] Parameters

out - iterator pointing to the first character to be overwritten
str - stream to retrieve the formatting information from
fill - padding character used when the results needs to be padded to the field width
val - value to convert to string and output

[edit] Return value

out

[edit] Notes

The leading zero generated by the conversion specification #o (resulting from the combination of std::showbase and std::oct for example) is not counted as a padding character.

When formatting a floating point value as hexfloat (i.e., when floatfield == (std::ios_base::fixed | std::ios_base::scientific)), the stream's precision is not used; instead, the number is always printed with enough precision to exactly represent the value. (since C++11)

[edit] Example

Output a number using the facet directly, and demonstrate user-defined facet:

#include #include   // this custom num_put outputs squares of all integers (except long long) struct squaring_num_put : std::num_put { iter_type do_put(iter_type out, std::ios_base& str, char_type fill, long val) const { return std::num_put::do_put(out, str, fill, val * val); }   iter_type do_put(iter_type out, std::ios_base& str, char_type fill, unsigned long val) const { return std::num_put::do_put(out, str, fill, val * val); } };   int main() { auto& facet = std::use_facet<std::num_put>(std::locale()); facet.put(std::cout, std::cout, '0', 2.71); std::cout << '\n';   std::cout.imbue(std::locale(std::cout.getloc(), new squaring_num_put)); std::cout << 6 << ' ' << -12 << '\n'; }

Output:

An implementation of operator<< for a user-defined type.

Output:

[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 34 C++98 the bool overload used non-existing memberstruename and falsename of std::ctype use these membersof std::numpunct
LWG 231 C++98 the precision modifier was only added if(flags & fixed) != 0 or str.precision() > 0 removed these conditions
LWG 282 C++98 the thousand separators were onlyinserted for integral types in stage 2 also inserted forfloating-point types
LWG 4084 C++11 "NAN" and "INF" could not be printed they can be printed

[edit] See also

| | inserts formatted data (public member function of std::basic_ostream<CharT,Traits>) [edit] | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |