#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <deque>
#include <type_traits>
using namespace std;
/*泛化版本*/
template<typename T>
class GetContainerType;
/*特化版本*/
template<typename T>
class GetContainerType<vector<T>> {
public:
GetContainerType()
{
std::cout << " vector " << std::endl;
}
using type = T;
};
/*特化版本*/
template<typename T>
class GetContainerType<list<T>> {
public:
GetContainerType()
{
std::cout << " list " << std::endl;
}
using type = T;
};
//萃取出数组元素个数
template <typename T, std::size_t Size>
class GetContainerType<T[Size]>{
public:
using type = T;
static const std::size_t size = Size;
};
int main()
{
std::cout << typeid(GetContainerType<std::list<int>>::type).name() <<std::endl;
std::cout << typeid(GetContainerType<float[20]>::type).name() << std::endl;
std::cout << GetContainerType<float[20]>::size << std::endl;
/*如果想专门用容器的类型,可以使用using type = typename T::value_type; 针对容器*/
return 0;
}
06-25
1092
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交