Skip to content

Latest commit

 

History

History
103 lines (83 loc) · 2.15 KB

xalloc.md

File metadata and controls

103 lines (83 loc) · 2.15 KB

xalloc

  • ios[meta header]
  • function[meta id-type]
  • std[meta namespace]
  • ios_base[meta class]
static int xalloc();

概要

私用記憶域を予約する。

この関数は、呼び出すたびに一意な整数値を返す。この関数は、IOマニピュレータ単位の設定状態を管理するために使用する。

戻り値

  • C++11まで
static int index = 0;
return index++;
  • C++14以降
    • スレッドセーフになった
static std::atomic<int> index(0);
return index++;
  • index++[link /reference/atomic/atomic/op_increment.md]

// 値を16進数で出力するIOマニピュレータを作成する
#include <iostream>
#include <string>
#include <sstream>

// 16進数マニピュレータ用の一意なIDを作成する
int hex_index()
{
  static int index = std::ios_base::xalloc();
  return index;
}

// 16進数マニピュレータ
std::ostream& hex_manip(std::ostream& os)
{
  // 16進数用のIDをキーとして、状態を設定する
  static bool state = false;
  state = true;
  os.pword(hex_index()) = &state;
  return os;
}

struct MyInt {
  int value = 0;
};

std::ostream& operator<<(std::ostream& os, const MyInt& x)
{
  // 16進数マニピュレータの状態を確認して、10進数と16進数どちらで出力するかを判定する
  void* state = os.pword(hex_index());
  if (!state || *static_cast<bool*>(state) == false) {
    os << x.value;
  }
  else {
    std::ostringstream ss;
    ss << std::hex << x.value;
    os << ss.str();
  }
  return os;
}

int main()
{
  MyInt x;
  x.value = 10;

  std::cout << x << std::endl;
  std::cout << hex_manip << x << std::endl;
}
  • std::ios_base::xalloc()[color ff0000]
  • std::ostream[link /reference/ostream/basic_ostream.md]
  • pword[link pword.md]
  • std::ostringstream[link /reference/sstream/basic_ostringstream.md]
  • std::hex[link /reference/ios/hex.md]

出力

10
a

参照