iostream マニピュレータ


https://qiita.com/ikiuo/items/2ae079438ef73965fc26

主に、これを弄るときの補足情報。

[&lt;iostream&gt;](https://cpprefjp.github.io/reference/iostream.html) と [&lt;iomanip&gt;](https://cpprefjp.github.io/reference/iomanip.html) から入出力マニピュレータを探してみます。

## [&lt;ios&gt;](https://cpprefjp.github.io/reference/ios.html) のマニピュレータ

- [boolalpha](https://cpprefjp.github.io/reference/ios/boolalpha.html) , [noboolalpha](https://cpprefjp.github.io/reference/ios/noboolalpha.html)
- [showbase](https://cpprefjp.github.io/reference/ios/showbase.html) , [noshowbase](https://cpprefjp.github.io/reference/ios/noshowbase.html)
- [showpoint](https://cpprefjp.github.io/reference/ios/showpoint.html) , [noshowpoint](https://cpprefjp.github.io/reference/ios/noshowpoint.html)
- [showpos](https://cpprefjp.github.io/reference/ios/showpos.html) , [noshowpos](https://cpprefjp.github.io/reference/ios/noshowpos.html)
- [skipws](https://cpprefjp.github.io/reference/ios/skipws.html) , [noskipws](https://cpprefjp.github.io/reference/ios/noskipws.html)
- [uppercase](https://cpprefjp.github.io/reference/ios/uppercase.html) , [nouppercase](https://cpprefjp.github.io/reference/ios/nouppercase.html)
- [unitbuf](https://cpprefjp.github.io/reference/ios/unitbuf.html) , [nounitbuf](https://cpprefjp.github.io/reference/ios/nounitbuf.html)
- [internal](https://cpprefjp.github.io/reference/ios/internal.html) , [left](https://cpprefjp.github.io/reference/ios/left.html) , [right](https://cpprefjp.github.io/reference/ios/right.html)
- [dec](https://cpprefjp.github.io/reference/ios/dec.html) , [hex](https://cpprefjp.github.io/reference/ios/hex.html) , [oct](https://cpprefjp.github.io/reference/ios/oct.html)
- [fixed](https://cpprefjp.github.io/reference/ios/fixed.html) , [scientific](https://cpprefjp.github.io/reference/ios/scientific.html) , [hexfloat](https://cpprefjp.github.io/reference/ios/hexfloat.html) , [defaultfloat](https://cpprefjp.github.io/reference/ios/defaultfloat.html)

<code>operator&lt;&lt;</code> 等で使用される型

```C++:
ios_base& (ios_base&);     // 関数
ios_base& (*)(ios_base&);  // 関数ポインタ
```

## [&lt;istream&gt;](https://cpprefjp.github.io/reference/istream.html) のマニピュレータ

- [ws](https://cpprefjp.github.io/reference/istream/ws.html)

<code>operator&gt;&gt;</code> 等で使用される型

```C++:
// 関数
template<class CharT, class Traits>
basic_istream<CharT, Traits>& (basic_istream<CharT, Traits>&);

// 関数ポインタ
template<class CharT, class Traits>
basic_istream<CharT, Traits>& (*)(basic_istream<CharT, Traits>&);
```


## [&lt;ostream&gt;](https://cpprefjp.github.io/reference/ostream.html) のマニピュレータ

- [endl](https://cpprefjp.github.io/reference/ostream/endl.html)
- [ends](https://cpprefjp.github.io/reference/ostream/ends.html)
- [flush](https://cpprefjp.github.io/reference/ostream/flush.html)
- [flush_emit](https://cpprefjp.github.io/reference/ostream/flush_emit.html)
- [emit_on_flush](https://cpprefjp.github.io/reference/ostream/emit_on_flush.html)
- [noemit_on_flush](https://cpprefjp.github.io/reference/ostream/noemit_on_flush.html)

<code>operator&lt;&lt;</code> 等で使用される型

```C++:
// 関数
template<class CharT, class Traits>
basic_ostream<CharT, Traits>& (basic_ostream<CharT, Traits>&);

// 関数ポインタ
template<class CharT, class Traits>
basic_ostream<CharT, Traits>& (*)(basic_ostream<CharT, Traits>&);
```

```C++:注意
std::cout << std::endl; // OK

// std::endl の型は確定していないのでコンパイル エラー
std::cout << typeid(std::endl).name();
```


## [&lt;iomanip&gt;](https://cpprefjp.github.io/reference/iomanip.html) のマニピュレータ

- [resetiosflags](https://cpprefjp.github.io/reference/iomanip/resetiosflags.html)
- [setiosflags](https://cpprefjp.github.io/reference/iomanip/setiosflags.html)
- [setbase](https://cpprefjp.github.io/reference/iomanip/setbase.html)
- [setfill](https://cpprefjp.github.io/reference/iomanip/setfill.html)
- [setprecision](https://cpprefjp.github.io/reference/iomanip/setprecision.html)
- [setw](https://cpprefjp.github.io/reference/iomanip/setw.html)
- [get_money](https://en.cppreference.com/w/cpp/io/manip/get_money)
- [put_money](https://en.cppreference.com/w/cpp/io/manip/put_money)
- [put_time](https://cpprefjp.github.io/reference/iomanip/put_time.html)
- [quoted](https://cpprefjp.github.io/reference/iomanip/quoted.html)

<code>operator&lt;&lt;</code> 等で使用される型は <code><var>unspecified</var></code> なので、実際の型(と<code>sizeof</code>)を調べる。

<details><summary>型表示プログラム</summary><div>

```C++:sample.cpp
#include <cwchar>
#include <iostream>
#include <iomanip>
#include <string>
#include <utility>

/**/

#if __clang__
#include <cxxabi.h>
#include <cstdlib>
static std::string demangle(const char* name)
{
    std::string demangled;
    size_t size = 1;
    char* buffer = (char*)std::malloc(size);
    int status = 0;

    char* res = abi::__cxa_demangle(name, buffer, &size, &status);
    if (res)
        demangled = res;
    else
    {
        demangled = name;
        res = buffer;
    }
    std::free(res);
    return demangled;
}
#else
inline static std::string demangle(const char* name)
{
    return std::string(name);
}
#endif

template <typename T>
inline static std::string gettypename(const T& x)
{
    return demangle(typeid(x).name());
}

template <typename T>
static void print_type(const char* msg, const T& x)
{
    std::cout << std::setw(21) << std::right << msg
              << "  " << std::setw(2) << sizeof(x)
              << "  " << std::left << gettypename(x)
              << std::endl;
}

int main()
{
    int money = 0;

    print_type("resetiosflags", std::resetiosflags(std::ios_base::fmtflags(0)));
    print_type("setiosflags", std::setiosflags(std::ios_base::fmtflags(0)));
    print_type("setbase(0)", std::setbase(0));
    print_type("setbase(long(0))", std::setbase(long(0)));
    print_type("setfill('0')", std::setfill('0'));
    print_type("setfill(L'0')", std::setfill(L'0'));
    print_type("setfill(u8'0')", std::setfill(u8'0'));
    print_type("setfill(u'0')", std::setfill(u'0'));
    print_type("setfill(U'0')", std::setfill(U'0'));
    print_type("setprecision(0)", std::setprecision(0));
    print_type("setprecision(long(0))", std::setprecision(long(0)));
    print_type("setw(0)", std::setw(0));
    print_type("setw(long(0))", std::setw(long(0)));
    print_type("get_money(int&)", std::get_money(money));
    print_type("put_money(int&)", std::put_money(money));
    print_type("get_time(0,\"\")", std::get_time(0, ""));
    print_type("get_time(0,L\"\")", std::get_time(0, L""));
    print_type("put_time(0,\"\")", std::put_time(0, ""));
    print_type("put_time(0,L\"\")", std::put_time(0, L""));
    print_type("quoted(\"\")", std::quoted(""));
    print_type("quoted(L\"\")", std::quoted(L""));

    return 0;
}
```

</div></details>


Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: x86_64-apple-darwin23.5.0

```console:実行結果
$ clang++ -std=c++20 -O1 sample.cpp -o sample && ./sample
        resetiosflags   4  std::__1::__iom_t1
          setiosflags   4  std::__1::__iom_t2
           setbase(0)   4  std::__1::__iom_t3
     setbase(long(0))   4  std::__1::__iom_t3
         setfill('0')   1  std::__1::__iom_t4<char>
        setfill(L'0')   4  std::__1::__iom_t4<wchar_t>
       setfill(u8'0')   1  std::__1::__iom_t4<char8_t>
        setfill(u'0')   2  std::__1::__iom_t4<char16_t>
        setfill(U'0')   4  std::__1::__iom_t4<char32_t>
      setprecision(0)   4  std::__1::__iom_t5
setprecision(long(0))   4  std::__1::__iom_t5
              setw(0)   4  std::__1::__iom_t6
        setw(long(0))   4  std::__1::__iom_t6
      get_money(int&)  16  std::__1::__iom_t7<int>
      put_money(int&)  16  std::__1::__iom_t8<int>
       get_time(0,"")  16  std::__1::__iom_t9<char>
      get_time(0,L"")  16  std::__1::__iom_t9<wchar_t>
       put_time(0,"")  16  std::__1::__iom_t10<char>
      put_time(0,L"")  16  std::__1::__iom_t10<wchar_t>
           quoted("")  24  std::__1::__quoted_output_proxy<char, void>
          quoted(L"")  24  std::__1::__quoted_output_proxy<wchar_t, void>
```

VisualStudio 2022 : Version 17.10.2
コンパイル オプションに <code>/std:c++20</code> を設定

```console:実行結果
        resetiosflags  16  struct std::_Smanip<int>
          setiosflags  16  struct std::_Smanip<int>
           setbase(0)  16  struct std::_Smanip<int>
     setbase(long(0))  16  struct std::_Smanip<int>
         setfill('0')   1  struct std::_Fillobj<char>
        setfill(L'0')   2  struct std::_Fillobj<wchar_t>
       setfill(u8'0')   1  struct std::_Fillobj<char8_t>
        setfill(u'0')   2  struct std::_Fillobj<char16_t>
        setfill(U'0')   4  struct std::_Fillobj<char32_t>
      setprecision(0)  16  struct std::_Smanip<__int64>
setprecision(long(0))  16  struct std::_Smanip<__int64>
              setw(0)  16  struct std::_Smanip<__int64>
        setw(long(0))  16  struct std::_Smanip<__int64>
      get_money(int&)  16  struct std::_Monobj<int>
      put_money(int&)  16  struct std::_Monobj<int const >
       get_time(0,"")  24  struct std::_Timeobj<char,struct tm * __ptr64>
      get_time(0,L"")  24  struct std::_Timeobj<wchar_t,struct tm * __ptr64>
       put_time(0,"")  24  struct std::_Timeobj<char,struct tm const * __ptr64>
      put_time(0,L"")  24  struct std::_Timeobj<wchar_t,struct tm const * __ptr64>
           quoted("")  24  struct std::_Quote_out<char,void,unsigned __int64>
          quoted(L"")  24  struct std::_Quote_out<wchar_t,void,unsigned __int64>
```