User defined Manipulator

In c++ hex,oct manipulators change a number to hex and oct.But I want make an user defined manipulator that will convert a integer output to binary.But I don't know how to pass the stream output integer number to the custom manipulator function or how to use that.Can anyone help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <type_traits>

namespace detail
{
    template <typename T > struct bits_
    {
        static_assert( std::is_integral<T>::value, "expected an integral type" ) ;
        using uint_type = typename std::make_unsigned<T>::type; // C++14: std::make_unsigned_t<T>
        uint_type number ;
        bool first_digit = true ;
        explicit bits_( uint_type n ) : number(n) {}
    };
}

template < typename CHAR_TYPE, typename TRAITS_TYPE, typename T >
std::basic_ostream<CHAR_TYPE,TRAITS_TYPE>&
operator<< ( std::basic_ostream<CHAR_TYPE,TRAITS_TYPE>& stm, detail::bits_<T> b )
{
    if( b.number == 0 && b.first_digit ) return stm << 0 ;
    else if( b.number == 1 ) return stm << +(b.number) ;
    else
    {
        b.first_digit = false ;
        const int last_digit = b.number%2 ;
        b.number /= 2 ;
        return stm << b << last_digit ;
    }
    // TODO: add support for showbase
}

template <typename T > detail::bits_<T> bits( T n ) { return detail::bits_<T>(n) ; }

int main()
{
    for( int i : { 0, 1023, 256, 100, 0x01000010, -1 } ) std::cout << bits(i) << '\n' ;

    std::cout << bits( char(-1) ) << '\n' << bits(18LL) << '\n' << bits( -1LL ) << '\n' ;
    std::wcout << bits( char(-1) ) << L'\n' << bits(18LL) << L'\n' << bits( -1LL ) << L'\n' ;
}

http://coliru.stacked-crooked.com/a/8b1d177d4f722e27
http://rextester.com/WRAY67602
Topic archived. No new replies allowed.