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

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


7

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.

Share

3

  • 1

    Your code doesn't call the manipulators, the I/O operators do. It's complicated 🙂 Look at operators 18-20 here. They call functions.

    – BoP

    13 hours ago


  • 2

    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.

    – Peter

    13 hours ago


  • 1

    "it does say on cplusplus.com that left is something called a "manipulator", but thats the first time I hear something like this, I have no idea what the term "manipulator" defines, nor wether anything on the website actually makes any sense" – Time to research…

    – Jesper Juhl

    13 hours ago

1 Answer
1

Reset to default


9

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);

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

Share
Improve this answer

5

  • 1

    I am getting confused still. is 'left' a function or a function pointer?

    – Anton Shustikov

    13 hours ago

  • 3

    @AntonShustikov std::left is a function, but the expression << std::left implicitly takes its address, as if by &std::left.

    – Jan Schultke

    13 hours ago

  • 3

    I cannot believe that I really couldn't figure this out myself. The way you worded everything and pointed out the correct thing for me made it go click. Your help is appreciated fondly!

    – Anton Shustikov

    13 hours ago

  • This is a very good imitation of a formula ChatGPT answer.

    – Sam Varshavchik

    11 hours ago

  • @SamVarshavchik it cited sources and didn't waste 4 paragraphs with background info.

    – qwr

    1 hour ago



Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *