enable_if
Applicability
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
x |
|
x |
|
x |
Function
Enables or disables specific function templates, class templates, or template specializations based on a condition during program building as a template metaprogramming tool defined in the <type_traits> header file. This implements more refined template overloading and type selection, enhancing the flexibility and security of code.
enable_if is a template structure with two template parameters. The template parameter Bp is a Boolean value, indicating a condition. The template parameter Tp is a type, and the default value is void. When Bp is set to false, enable_if has no nested type member. When Bp is set to true, enable_if has a nested type member whose type is Tp.
Prototype
1 2 | template <bool Bp, typename Tp> struct enable_if; |
Parameters
Parameter |
Description |
|---|---|
Bp |
Boolean value, indicating a condition. |
Tp |
Type. The default value is void. |
Restrictions
None
Returns
The static constant member type of enable_if is used to obtain the return value. The values of enable_if<Bp, Tp>::type are as follows:
- Tp: Bp is set to true.
- void: Bp is set to false.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | template <typename T> class Calculator { public: // Enable this member function when T is an integer type. template <typename U = T> typename AscendC::Std::enable_if<AscendC::Std::is_integral<U>::value, U>::type __aicore__ inline multiply(U a, U b) { AscendC::PRINTF("Integral type multiplication"); return a * b; } // Enable this member function when T is not an integer type. template <typename U = T> typename AscendC::Std::enable_if<!AscendC::Std::is_integral<U>::value, U>::type __aicore__ inline multiply(U a, U b) { AscendC::PRINTF("Non-integral type multiplication"); return a * b; } }; // Common template class template <typename T, typename Enable = void> class Container { public: __aicore__ inline Container() { AscendC::PRINTF("Generic container.\n"); } }; // Specialized version. This version is enabled when T is an integer type. template <typename T> class Container<T, typename AscendC::Std::enable_if<AscendC::Std::is_integral<T>::value>::type> { public: __aicore__ inline Container() { AscendC::PRINTF("Integral container.\n"); } }; // Enable this function when T is an integer type. template <typename T> __aicore__ inline typename AscendC::Std::enable_if<AscendC::Std::is_integral<T>::value, T>::type add(T a, T b) { AscendC::PRINTF("Integral type addition."); return a + b; } // Enable this function when T is not an integer type. template <typename T> __aicore__ inline typename AscendC::Std::enable_if<!AscendC::Std::is_integral<T>::value, T>::type add(T a, T b) { AscendC::PRINTF("Non-integral type addition."); return a + (-b); } Calculator<int> intCalculator; int intResult = intCalculator.multiply((int)2, (int)3); AscendC::PRINTF("Result of integral multiplication: %d\n", intResult); Calculator<float> doubleCalculator; float doubleResult = doubleCalculator.multiply((float)2.5, (float)3.5); AscendC::PRINTF("Result of non-integral multiplication: %f\n", doubleResult); Container<float> genericContainer; Container<int> integralContainer; intResult = add(1, 2); AscendC::PRINTF("Integer result: %d\n", intResult); doubleResult = add((float)1.5, (float)2.5); AscendC::PRINTF("float result: %f\n", doubleResult); |
1 2 3 4 5 6 7 | // Execution result: Integral type multiplicationResult of integral multiplication: 6 Non-integral type multiplicationResult of non-integral multiplication: 8.750000 Generic container. Integral container. Integral type addition.Integer result: 3 Non-integral type addition.float result: -1.000000 |