How are iomanip Functions Implemented?

后端 未结 1 1029
盖世英雄少女心
盖世英雄少女心 2021-01-05 05:02

Some of the standard iomanip functions take take a parameter.

I\'d like to know how this is accomplished, for instance, can I do something similar with a

相关标签:
1条回答
  • 2021-01-05 06:01

    Here is a simple example of a user-defined manipulator that takes one parameter defined using a class:

    #include <iostream>
    
    class putX // injects some `X`s into the stream
    {
        std::size_t _n;
    public:
        explicit putX(std::size_t n): _n(n) {}
        std::size_t getn() const {return _n;}
        friend std::ostream& operator<<(std::ostream& os, const putX& obj)
        {
            std::size_t n = obj.getn();
            for (std::size_t i = 0; i < n; ++i)
                os << 'X';
            return os;
        }
    };
    
    int main()
    {
        std::cout << putX(10) << " test " << putX(10);
    }
    

    Manipulators that take no parameters can simply be implemented as

    std::ostream& custom_manip(std::ostream& os) { // do something with os and return os;}
    

    That's because basic_ostream::operator<< has an overload that takes a pointer-to-function std::ostream& (*fp)(std::ostream&) as its right hand side (e.g., a manipulator)

    PS: The C++ Standard Library by N. Josuttis describes how manipulators/custom manipulators work in great detail, see Sec. 15.6.3 User-Defined Manipulators

    0 讨论(0)
提交回复
热议问题