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

履歴 編集

class template
<type_traits>

std::integral_constant(C++11)

namespace std {
  template <class T, T v>
  struct integral_constant {
    static constexpr T value = v;

    using value_type = T;
    using type       = integral_constant<T,v>;

    constexpr operator value_type()          { return value; } // C++11
    constexpr operator value_type() noexcept { return value; } // C++14

    constexpr value_type operator()() const noexcept { return value; } // C++14
  };
}

概要

integral_constant は基本となる整数型と定数を合わせ,型として整数定数を表す。

多くの場合、<type_traits> 内のクラスやその他クラスから基底クラスとして派生されることによって用いられる。

#include <type_traits>

using int_zero = std::integral_constant<int, 0>;

static_assert(int_zero::value == 0, "value == 0");
static_assert(std::is_same<int_zero::value_type, int>::value, "value_type == int");
static_assert(std::is_same<int_zero::type, int_zero>::value, "type == int_zero");
static_assert(int_zero() == 0, "int_zero() == 0");

int main(){}

出力

バージョン

言語

  • C++11

処理系

  • GCC: 4.3.4, 4.5.3, 4.6.2, 4.7.0
  • Visual C++: 2008 (std::tr1), 2010, 2012, 2013, 2015
    • operator value_type関数の実装状況は次のとおり。
      • 2012~2013は、operator value_type() constと定義されている。constexprnoexcept修飾がされていない。
      • 2015では、C++14の仕様どおりに実装されている。
    • operator()関数は、2015から実装されている。

備考

上の例でコンパイラによってはエラーになる。GCC 4.3.4, 4.5.3, Visual C++ 2010 は integral_constantoperator value_type() を持っていないためエラーになる。

関連項目

参照