CKTIS / C++
std::setfill — output field fill character
std::setfill from <iomanip> sets the character used to pad fields during formatted output. It works with a field width, for example one set by std::setw, and its setting remains active until changed again.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <iomanip>
// Declaration (unspecified denotes an unspecified return type):
template<class charT> unspecified setfill(charT c);
// Usage:
out << std::setfill(c);std::setfill(c) is a manipulator from <iomanip>. The expression out << std::setfill(c) changes the stream state as if calling out.fill(c); it does not itself write characters. The function returns an object of unspecified type, while the complete expression using << returns a reference to out, allowing operations to be chained.
When formatted output needs padding to reach a field width, the stream uses the configured character. std::setw(n) specifies a minimum width, not a length limit: longer text is not truncated. For text, std::right places padding before the content and std::left places it after the content. std::setfill alone sets neither width nor alignment.
The fill character remains set for subsequent operations, whereas character and string insertion reset the field width to zero. Therefore, repeat std::setw before each field that needs padding. When sharing a stream, save the previous character with out.fill() and restore it with out.fill(previous); this does not restore other formatting settings.
The type of c must match the stream's character type: use, for example, '.' for a char stream and L'.' for a wchar_t stream. The example demonstrates fill-character persistence and both alignment directions. The C++ working draft specifies std::setfill in [std.manip].
Examples
#include <iomanip>
#include <iostream>
#include <sstream>
int main() {
std::ostringstream out;
const char previous = out.fill();
out << std::setfill('.') << std::right
<< std::setw(5) << "A" << '|'
<< std::setw(5) << "BC" << '|'
<< std::left << std::setw(5) << "D";
out.fill(previous);
std::cout << out.str() << '\n';
}
Result
....A|...BC|D....
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.