c++iostreamoutputmanipulators

Passing std::left as an argument


I have a function that display something, where the character positioning can be different. Function looks like this:

void display(std::ostream & stream, std::ios_base & positioning);

However, when I try to call this function like this

display_header(std::cout, std::left);

I get the following error

error: non-const lvalue reference to type 'std::ios_base' cannot bind to a value of unrelated type 'std::ios_base &(std::ios_base &)'

I don't understand why std::cout can be passed just fine, but std::left refuses to do so. Also how is lvalue reference to type 'std::ios_base' different from lvalue reference to type 'std::ios_base' ?


Solution

  • Like most stream manipulators, std::left is a function, not a variable. display would need to be changed to

    void display(std::ostream & stream, std::ios_base &(*positioning)(std::ios_base &));
    

    In order to accept left, right, internal, or any other manipulator of the type std::ios_base &(std::ios_base &).