产品 |
是否支持 |
|---|---|
Ascend 950PR/Ascend 950DT |
√ |
√ |
|
√ |
|
x |
|
x |
|
x |
|
x |
在程序编译时,检测一个类型是否为const限定的类型,可以用于在编译时进行类型检查和条件处理。
1 2 | template <typename Tp> struct is_const; |
参数名 |
含义 |
|---|---|
Tp |
需要检测的类型,包括基本类型(如int、float等)、复合类型(如数组、指针、引用)、用户自定义类型(如类、结构体等),以及const限定的类型本身。 |
无
is_const的静态常量成员value用于获取返回的布尔值,is_const<Tp>::value取值如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // 自定义测试类型 struct MyStruct{int val;}; // Mismatch scenario AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<int>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<const int*>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<int&>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<const int&>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<int&&>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<int[5]>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<double>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<MyStruct>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<void>::value); // Matching scenario AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<const int>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<int const>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<int* const>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<const int[5]>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<const volatile double>::value); AscendC::printf("AscendC::Std::is_const::value:%d\n", AscendC::Std::is_const<const MyStruct>::value); |
// 执行结果: AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:0 AscendC::Std::is_const::value:1 AscendC::Std::is_const::value:1 AscendC::Std::is_const::value:1 AscendC::Std::is_const::value:1 AscendC::Std::is_const::value:1 AscendC::Std::is_const::value:1