stanna23
stanna23

Reputation: 141

What is the purpose of ios::adjustfield?

What exactly is ios::adjustfield and what does it do? When and how would I go about making use of it?

Upvotes: 0

Views: 794

Answers (1)

T.C.
T.C.

Reputation: 137345

ios::adjustfield is a bit mask that is made by OR'ing together ios::left, ios::right, and ios::internal. The latter three are flags that control the adjustment of the output. This page has a nice example of what those three flags do.

It doesn't make much sense to set ios::adjustfield itself, though - it doesn't make any sense to set more than one of the three adjust flags at any given time. So why does the standard library provide it?

The answer is to make it easy to clear existing adjust flags. If flg is the current set of formatting flags, you can't set ios::right simply by flg |= ios::right;, because if a different adjustment flag is already set, you just set two adjustment flags on at the same time, which is nonsensical.

Instead, you need to clear the current adjustment flags first with flg &= ~ios::adjustfield; This clears the way for you to then set another adjustment flag with flg |= ios::right; The function ios::setf(), which manipulates the ios flags, allows you to give it a mask of flags to be cleared.

Note that you usually don't need to use ios::adjustfield directly; the I/O manipulators std::left, std::right and std::internal are far more convenient.

Upvotes: 1

Related Questions