std::integral_constant
Определено в заголовке <type_traits> | ||
|---|---|---|
template< class T, T v > struct integral_constant; | (с C++11) |
std::integral_constant оборачивает стальную константу заданного типа. Это базовый класс для шаблонов типов C++.
Поведение программы, добавляющей специализации для std::integral_constant является неопределённым.
Вспомогательные шаблоны псевдонимов
Вспомогательный шаблон псевдонима std::bool_constant определён для распространённого случая, когда T является bool.
template< bool B > using bool_constant = integral_constant<bool, B>; | (с C++17) |
Специализации
Предоставлены два typedef для распространённого случая, когда T является bool:
Определено в заголовке <type_traits> |
|
|---|---|
| Тип | Определение |
true_type | std::integral_constant<bool, true> |
false_type | std::integral_constant<bool, false> |
Члены-типы
| Тип | Определение |
|---|---|
value_type | T |
type | std::integral_constant<T, v> |
Члены-константы
| Имя | Значение |
|---|---|
| constexpr T value
[static] | Статическая константа типа T со значением v (публичная статическая константа) |
Члены-функции
| operator value_type | возвращает обернутое значение (публичная функция-член) |
| operator()
(C++14) | возвращает обернутое значение (публичная функция-член) |
std::integral_constant::operator value_type
constexpr operator value_type() const noexcept; |
Функция преобразования. Возвращает обернутое значение.
std::integral_constant::operator()
constexpr value_type operator()() const noexcept; | (с C++14) |
Возвращает обернутое значение. Эта функция позволяет std::integral_constant выступать в роли источника объектов функций времени компиляции.
Возможная реализация
template<class T, T v>
struct integral_constant
{
static constexpr T value = v;
using value_type = T;
using type = integral_constant; // using injected-class-name
constexpr operator value_type() const noexcept { return value; }
constexpr value_type operator()() const noexcept { return value; } // since c++14
}; |
Примечания
| Макрос проверки наличия функции | Значение | Стандарт | Функция |
|---|---|---|---|
__cpp_lib_integral_constant_callable | 201304L | (C++14) |
std::integral_constant::operator() |
__cpp_lib_bool_constant | 201505L | (C++17) |
std::bool_constant |
Пример
#include <type_traits>
int main()
{
typedef std::integral_constant<int, 2> two_t;
typedef std::integral_constant<int, 4> four_t;
static_assert(not std::is_same<two_t, four_t>::value,
"two_t and four_t are equal!");
static_assert(two_t::value * 2 == four_t::value, "2*2 != 4");
enum class my_e { e1, e2 };
typedef std::integral_constant<my_e, my_e::e1> my_e_e1;
typedef std::integral_constant<my_e, my_e::e2> my_e_e2;
static_assert(my_e_e1() == my_e::e1);
static_assert(my_e_e1::value != my_e::e2,
"my_e_e1::value == my_e::e2");
static_assert(std::is_same<my_e_e2, my_e_e2>::value,
"my_e_e2 != my_e_e2");
}
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/types/integral_constant