最終更新日時(UTC):
が更新

履歴 編集

variable
<iterator>

std::is-integer-like(C++20)

namespace std {
  template<class T>
  inline constexpr bool is-integer-like = /*see below*/;

  template<class T>
  inline constexpr bool is-signed-integer-like = /*see below*/;
}

概要

これらの説明専用の変数テンプレートは、任意の型Tが符号付/なし整数型そのものあるいはそれと同様に扱える型である場合にtrueを示すものである。

これは標準ライブラリ内において、イテレータの差分型(difference_type)として実装定義の整数型の使用を許可するためにコンセプトの文脈で使用される。

効果

is-integer-like<T>Tintegralのモデルとなるかinteger-classである時にtrueを示す。ただし、TboolかそのCV修飾された型である場合は常にfalseとなる。
is-signed-integer-like<T>Tinteger-likeis-integer-like<T> == true)であり、signed_integralのモデルとなるかsigned-integer-classである時にtrueを示す。

(signed-)integer-class型

integer-class型は組み込みの整数型と同じように動作する実装定義のクラス型である。

integer-class型の表現可能な範囲はそれが定義する値の連続集合であり、01を必ず含んでいなければならない。この時、その範囲に負の数が含まれていればその型はsigned-integer-class型であり、それ以外のものはunsigned-integer-class型である。

要件

Iをあるinteger-class型、Bを少なくともIと同じ範囲の値を表現可能で同じ幅を持つ別のinteger-class型とする。
Iの値a, ba, bそれぞれと同じ値を表現するBの値x, yと任意の整数型の値cについて次のことが成り立つ。

  • @x適格である全ての単項演算子@について@aもまた適格であり、共に同じ値、効果、値カテゴリを持つ。@xbool型を示す場合@aもまたbool型を示すが。@xBを示す場合は@aIを示す。
  • c @= x適格である全ての(複合)代入演算子@=についてc @= aもまた適格であり、共に同じ値、効果を持つ。式c @= aの結果はcを参照するlvalueとなる。
  • x @ y適格である全ての二項演算子@についてa @ bもまた適格であり、結果の値をIが表現できる場合は共に同じ値、効果、値カテゴリを持つ。x @ ybool型を示す場合a @ bもまたbool型を示すが。x @ yBを示す場合はa @ bIを示す。
  • integer-class型の値は任意の整数型に明示的に変換でき、任意の整数型の値はinteger-class型に暗黙的にも明示的にも変換できる。それらの変換は例外を送出しない。
  • integer-class型の式Ebool(E != I(0))のように文脈的にboolに変換できる。
  • integer-class型はregular及びtotally_orderedのモデルとなる。
  • 値初期化されたinteger-class型の値は0になる。

integer-classIについてnumeric_limits<I>の特殊化は次のような値を示す。

実装例 (MSVC)

inline constexpr bool _Is_nonbool_integral = is_integral_v<_Ty> && !is_same_v<remove_cv_t<_Ty>, bool>;

template <class _Ty>
inline constexpr bool _Integer_class = requires {
  typename _Ty::_Signed_type;
  typename _Ty::_Unsigned_type;
};

template <class _Ty>
concept _Integer_like = _Is_nonbool_integral<remove_cv_t<_Ty>> || _Integer_class<_Ty>;

template <class _Ty>
concept _Signed_integer_like = _Integer_like<_Ty> && static_cast<_Ty>(-1) < static_cast<_Ty>(0);

上記の説明のうち構文的な要件を素直に実装している。MSVCでは、128ビット整数がinteger-class型である。

バージョン

言語

  • C++20

関連項目

参照