integral_constant

Applicability

Product

Supported

Atlas A3 training products/Atlas A3 inference products

Atlas A2 training products/Atlas A2 inference products

Atlas 200I/500 A2 inference products

x

Atlas inference product's AI Core

x

Atlas inference product's Vector Core

x

Atlas training products

x

Function

integral_constant is a struct with template parameters, defined in the <type_traits> header file. It is used to encapsulate a compile constant integer value and is a fundamental component for many type traits and compile-time computations in the standard library.

The functions of integral_constant are as follows:

  1. Encapsulate a compile-time constant by converting a value of the int or bool type into a specific type, allowing the value to be manipulated and passed during compilation.
  2. Identifies a type. Each integral_constant instance is a unique type, which can be used for template specialization or overload resolution.
  3. Supports implicit conversion to the encapsulated value type, allowing the value to be directly used in the context where it is needed.
  4. The function call operator allows an instance to be called like a function to obtain its value.

integral_constant provides multiple commonly used specializations, as described in the following:

  • Std::true_type: alias of integral_constant<bool, true>.
  • Std::false_type: alias of integral_constant<bool, false>.
  • Numeric constant, for example, Std::integral_constant<int, 42>.
  • A simplified notation of numeric constants, for example, Std::Int<42>. The numeric type is size_t.

Prototype

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
template <typename Tp, Tp v>
struct integral_constant
{
    static constexpr const Tp value = v;
    using value_type = Tp;
    using type = integral_constant;
    inline constexpr operator value_type() const noexcept;
    inline constexpr value_type operator()() const noexcept;
};

template <size_t v>
using Int = integral_constant<size_t, v>;

Parameters

Table 1 Template parameters

Parameter

Description

Tp

Data type of the value.

v

Constant value.

Restrictions

  • The Int type is an alias for the integral_constant value structure. The value type must be of the size_t type.
  • The template parameter Tp does not support floating-point types such as float because the template parameter needs to be determined at the compilation stage, and the precision of floating-point numbers may cause the failure to accurately represent the template parameter at the compilation stage.

Returns

None

Example

  • Numerical type encapsulation
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    // The following is an example of a UT based on GoogleTest.
    using IntTrue = AscendC::Std::integral_constant<int, 1>;
    using IntFalse = AscendC::Std::integral_constant<int, 0>;
    // Test the static constant value.
    EXPECT_EQ(IntTrue::value, 1);
    EXPECT_EQ(IntFalse::value, 0);
    // Test the () operator overloading.
    EXPECT_EQ(IntTrue()(), 1);
    EXPECT_EQ(IntFalse()(), 0);
    // Test the type definition.
    EXPECT_TRUE((AscendC::Std::is_same<typename IntTrue::value_type, int>::value));
    EXPECT_TRUE((AscendC::Std::is_same<typename IntTrue::type, IntTrue>::value));
    
  • Specialized type — bool
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    // The following is an example of a UT based on GoogleTest.
    using TrueType = AscendC::Std::true_type;
    using FalseType = AscendC::Std::false_type;
    // Test the static constant value.
    EXPECT_TRUE(TrueType::value);
    EXPECT_FALSE(FalseType::value);
    // Test the () operator overloading.
    EXPECT_TRUE(TrueType()());
    EXPECT_FALSE(FalseType()());
    // Test the type definition.
    EXPECT_TRUE((AscendC::Std::is_same<typename TrueType::value_type, bool>::value));
    EXPECT_TRUE((AscendC::Std::is_same<typename TrueType::type, TrueType>::value));
    EXPECT_TRUE((AscendC::Std::is_same<typename FalseType::type, FalseType>::value));
    
  • Specialized type — Int
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    // The following is an example of a UT based on GoogleTest.
    using Zero = AscendC::Std::Int<0>;
    using One = AscendC::Std::Int<1>;
    using Large = AscendC::Std::Int<0xFFFFFFFF>;
    // Verify the static constant value.
    EXPECT_EQ(Zero::value, 0);
    EXPECT_EQ(One::value, 1);
    EXPECT_EQ(Large::value, 0xFFFFFFFF);
    // Verify the type definition.
    EXPECT_TRUE((AscendC::Std::is_same<typename Zero::value_type, size_t>::value));
    EXPECT_TRUE((AscendC::Std::is_same<typename Zero::type, Zero>::value));
    EXPECT_TRUE((AscendC::Std::is_same<Zero, AscendC::Std::integral_constant<size_t, 0>>::value));
    // Verify the () operator overloading.
    EXPECT_EQ(Zero()(), 0);
    EXPECT_EQ(One()(), 1);
    EXPECT_EQ(Large()(), 0xFFFFFFFF);
    
  • Operations of the Int specialized type
    1
    2
    3
    4
    5
    6
    7
    // Addition
    static_assert((AscendC::Std::Int<5>::value + AscendC::Std::Int<3>::value) == 8, "Addition failed");
    // Multiplication
    static_assert((AscendC::Std::Int<4>::value * AscendC::Std::Int<6>::value) == 24, "Multiplication failed");
    //Comparison
    static_assert(AscendC::Std::Int<10>::value > AscendC::Std::Int<5>::value, "Comparison failed");
    static_assert(AscendC::Std::Int<7>::value != AscendC::Std::Int<77>::value, "Equality check failed");