0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

iostream マニピュレータ

Last updated at Posted at 2024-06-24

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

<iostream><iomanip> から入出力マニピュレータを探してみます。

<ios> のマニピュレータ

operator<< 等で使用される型

ios_base& (ios_base&);     // 関数
ios_base& (*)(ios_base&);  // 関数ポインタ

<istream> のマニピュレータ

operator>> 等で使用される型

// 関数
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>&);

<ostream> のマニピュレータ

operator<< 等で使用される型

// 関数
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>&);
注意
std::cout << std::endl; // OK

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

<iomanip> のマニピュレータ

operator<< 等で使用される型は unspecified なので、実際の型(とsizeof)を調べる。

型表示プログラム
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;
}

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

実行結果
$ 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
コンパイル オプションに /std:c++20 を設定

実行結果
        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>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?