contains<>
时间: 2023-07-08 20:05:16 浏览: 43
`contains<>` is a type trait in C++ that checks if a type pack contains a specific type. It is defined in the `<type_traits>` header.
Here is an example usage of `contains<>`:
```c++
#include <iostream>
#include <type_traits>
template<typename... Args>
struct MyStruct {
static constexpr bool has_int = std::disjunction_v<std::is_same<int, Args>...>;
};
int main() {
std::cout << std::boolalpha;
std::cout << MyStruct<char, bool, float>::has_int << '\n'; // false
std::cout << MyStruct<char, bool, int, float>::has_int << '\n'; // true
return 0;
}
```
In this example, `MyStruct` is a class template that takes a variadic pack of types. The `has_int` member variable is a `bool` that indicates whether the pack contains the type `int`.
The `std::is_same` trait is used to check for equality between types, and the `std::disjunction` trait is used to combine the results of multiple `std::is_same` checks into a single `bool` value. The `std::disjunction_v` alias is a shorthand for calling `std::disjunction::value`.