how to use std::enable_if<>
时间: 2024-01-22 21:02:26 浏览: 33
std::enable_if<> is a template utility in C++ that enables or disables certain functions or templates based on type traits. It takes two template arguments: a boolean condition and a return type. If the boolean condition is true, it returns the return type, otherwise it doesn't compile.
Here's an example of how to use std::enable_if<>:
```
template <typename T>
typename std::enable_if<std::is_integral<T>::value, bool>::type
is_odd(T i) {
return i % 2 != 0;
}
```
In the above example, the function is_odd() takes a template parameter T, and returns a bool. However, it only compiles if std::is_integral<T>::value is true, i.e. T is an integer type. If T is not an integer type, the function will not be compiled.
I hope this helps! Let me know if you have any other questions.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)