CKTIS / C++
std::integral_constant — a constant as a type
std::integral_constant<T, v> represents the constant v of type T as a type. The <type_traits> header also provides std::bool_constant, std::true_type and std::false_type for expressing Boolean results in metaprogramming.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
#include <type_traits>
// Synopsis only: the header provides these declarations.
namespace std {
template<class T, T v> struct integral_constant;
template<bool B> using bool_constant = integral_constant<bool, B>;
using true_type = bool_constant<true>;
using false_type = bool_constant<false>;
}std::integral_constant<T, v> from <type_traits> associates the constant v of type T with a type. It is a building block of the type-trait interface: a result can be passed as a type rather than just a value.
The member value is a static constexpr T equal to v; value_type names T, and type names the specialization std::integral_constant<T, v> itself. An object provides an implicit conversion to value_type and an operator(), both constexpr and noexcept, returning value.
std::bool_constant<B> aliases std::integral_constant<bool, B>. std::true_type and std::false_type name std::bool_constant<true> and std::bool_constant<false>, respectively; they are types, not Boolean variables. The syntax section shows illustrative declarations—include the header rather than declaring these names yourself.
Examples
#include <iostream>
#include <type_traits>
int main() {
using Answer = std::integral_constant<int, 42>;
using Positive = std::bool_constant<(Answer::value > 0)>;
static_assert(std::is_same_v<Answer::value_type, int>);
static_assert(std::is_same_v<Answer::type, Answer>);
static_assert(std::is_same_v<Positive, std::true_type>);
static_assert(!std::false_type::value);
static_assert(Answer{}() == 42);
constexpr int converted = Answer{};
std::cout << Answer::value << ' ' << converted << ' '
<< std::boolalpha << Positive{}() << ' '
<< std::false_type{}() << '\n';
}Result
42 42 true false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.