How do the std::left and std::right I/O manipulators work, and why are they used in the way they are used?
Asked Answered
E

1

8

I am learning C++ and my goal is to have a table beautifully displayed in console. I tried using std::left and std::right I/O manipulators, but now that I look at my code I cannot figure out what exactly these things are and what kind of mechanism they employ.

So far, every function call required me to at least put empty brackets () after the function name, so there could be no confusion for what is and isn't a function.

cplusplus.com and cppreference.com gave me an understanding that left and right are indeed functions, but those can be called without using brackets.

In short, why can I just put left anywhere like this without brackets, but any other function call requires me to have brackets ()?

Also, it says on cplusplus.com that left is something called a "manipulator", but that's the first time I heard something like this. I have no idea what the term "manipulator" defines, nor whether anything on the website actually makes any sense.

Exurb answered 15/8, 2023 at 14:4 Comment(2)
Your code doesn't call the manipulators, the I/O operators do. It's complicated :-) Look at operators 18-20 here. They call functions.Prospector
Look at information at en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt about member functions named operator<<() supported by templated class std::basic_ostream (std::ostream, and a bunch of other output stream types are specialisations of std::basic_ostream). There are a heap of overloads, but three of them (numbers 18 to 20 at the link) are functions. Each of those overloads calls the functions passed to them, and are used to implement IO manipulators. One of those overloads will accept left, call the passed function, which will perform required actions.Bohn
H
13

The std::left and std::right I/O manipulators1) set the alignment of fields for I/O streams. To see the effect, you also have to set the field width using std::setw. For example:

#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::left  << '{' << std::setw(10) << "left" << "}\n"
              << std::right << '{' << std::setw(10) << "right" << '}';
}

This outputs:

{left      }
{     right}

I/O manipulators are functions, and could be called with (), but are usually called by the overloaded << operator which takes these manipulators as function pointers:

// func is a pointer to a function which accepts a reference
// to std.:ios_base, and returns a reference to std::ios_base
basic_ostream& operator<<(std::ios_base& (*func)(std::ios_base&)); // (18)

// I/O manipulators are functions:
//   std::ios_base& left( std::ios_base& str );

What actually happens in std::cout << std::left is:

std::cout.operator<<(&std::left);
// equivalent to
std::left(std::cout);

1) I/O manipulators are the functions in the <iomanip> header.

Hokeypokey answered 15/8, 2023 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.