次の方法で共有


integral_constant Class, bool_constant Class

 

The latest version of this topic can be found at integral_constant Class, bool_constant Class.

Makes an integral constant from a type and value.

Syntax

struct integral_constant {
static constexpr T value = v;
typedef T value_type;
typedef integral_constant<T, v>
type;
constexpr operator value_type() const noexcept { return (value); }
constexpr value_type operator()() const noexcept { return (value); }
};

Parameters

T
The type of the constant.

v
The value of the constant.

Remarks

The integral_constant template class, when specialized with an integral type T and a value v of that type, represents an object that holds a constant of that integral type with the specified value. The member named type is an alias for the generated template specialization type, and the value member holds the value v used to create the specialization.

The bool_constant template class is an explicit partial specialization of integral_constant that uses bool as the T argument.

Example

// std__type_traits__integral_constant.cpp   
// compile with: /EHsc   
#include <type_traits>   
#include <iostream>   
  
int main()   
    {   
    std::cout << "integral_constant<int, 5> == "   
        << std::integral_constant<int, 5>::value << std::endl;   
    std::cout << "integral_constant<bool, false> == " << std::boolalpha   
        << std::integral_constant<bool, false>::value << std::endl;   
  
    return (0);   
    }  
  
integral_constant<int, 5> == 5  
integral_constant<bool, false> == false  

Requirements

Header: <type_traits>

Namespace: std

See Also

<type_traits>
false_type Typedef
true_type Typedef