产品 |
是否支持 |
|---|---|
Ascend 950PR/Ascend 950DT |
√ |
√ |
|
√ |
|
x |
|
x |
|
x |
|
x |
integral_constant是一个带有模板参数的结构体,定义在<type_traits>头文件中,用于封装一个编译时常量整数值,是标准库中许多类型特性和编译时计算的基础组件。
integral_constant的功能如下:
integral_constant提供了多个常用的特化版本,具体如下:
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>; |
参数名 |
含义 |
|---|---|
Tp |
数值的数据类型。 |
v |
常量数值。 |
无
1 2 3 4 5 6 7 8 9 10 11 12 | // 以下示例为基于googletest的UT示例 using IntTrue = AscendC::Std::integral_constant<int, 1>; using IntFalse = AscendC::Std::integral_constant<int, 0>; // 测试 value 静态常量 EXPECT_EQ(IntTrue::value, 1); EXPECT_EQ(IntFalse::value, 0); // 测试()操作符重载 EXPECT_EQ(IntTrue()(), 1); EXPECT_EQ(IntFalse()(), 0); // 测试类型定义 EXPECT_TRUE((AscendC::Std::is_same<typename IntTrue::value_type, int>::value)); EXPECT_TRUE((AscendC::Std::is_same<typename IntTrue::type, IntTrue>::value)); |
1 2 3 4 5 6 7 8 9 10 11 12 13 | // 以下示例为基于googletest的UT示例 using TrueType = AscendC::Std::true_type; using FalseType = AscendC::Std::false_type; // 测试 value 静态常量 EXPECT_TRUE(TrueType::value); EXPECT_FALSE(FalseType::value); // 测试()操作符重载 EXPECT_TRUE(TrueType()()); EXPECT_FALSE(FalseType()()); // 测试类型定义 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)); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // 以下示例为基于googletest的UT示例 using Zero = AscendC::Std::Int<0>; using One = AscendC::Std::Int<1>; using Large = AscendC::Std::Int<0xFFFFFFFF>; // 验证 value 静态常量 EXPECT_EQ(Zero::value, 0); EXPECT_EQ(One::value, 1); EXPECT_EQ(Large::value, 0xFFFFFFFF); // 验证类型定义 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)); // 验证()操作符重载 EXPECT_EQ(Zero()(), 0); EXPECT_EQ(One()(), 1); EXPECT_EQ(Large()(), 0xFFFFFFFF); |
1 2 3 4 5 6 7 | // 加法 static_assert((AscendC::Std::Int<5>::value + AscendC::Std::Int<3>::value) == 8, "Addition failed"); // 乘法 static_assert((AscendC::Std::Int<4>::value * AscendC::Std::Int<6>::value) == 24, "Multiplication failed"); // 比较 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"); |