remove_extent
#include <iostream>
#include <memory>
#include <type_traits>
#include <typeinfo>
template<class T>
struct remove_extent { typedef T type; };
template<class T>
struct remove_extent<T[]> { typedef T type; };
template<class T, std::size_t N>
struct remove_extent<T[N]> { typedef T type; };
int main()
{
std::cout << typeid(remove_extent<int>::type).name() << std::endl;
std::cout << typeid(remove_extent<int[]>::type).name() << std::endl;
std::cout << typeid(remove_extent<int[5]>::type).name() << std::endl;
return 0;
}
输出结果为:
:g++ -std=c++11 main.cc -o main
:./main | c++filt
int
int
int
std::is_array
#include <iostream>
#include <memory>
#include <type_traits>
#include <typeinfo>
template<class T>
struct is_array : std::false_type {};
template<class T>
struct is_array<T[]> : std::true_type {};
template<class T, std::size_t N>
struct is_array<T[N]> : std::true_type {};
int main()
{
using int_array_type = int[5];
std::cout << is_array<int>::value << std::endl;
std::cout << is_array<int[]>::value << std::endl;
std::cout << is_array<int[5]>::value << std::endl;
std::cout << is_array<int_array_type>::value << std::endl;
return 0;
}
输出结果为:
:g++ -std=c++11 main.cc -o main
:./main | c++filt
0
1
1
1
std::rank
#include <iostream>
#include <memory>
#include <type_traits>
#include <typeinfo>
template<class T>
struct rank : public std::integral_constant<std::size_t, 0> {};
template<class T>
struct rank<T[]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {};
template<class T, std::size_t N>
struct rank<T[N]> : public std::integral_constant<std::size_t, rank<T>::value + 1> {};
int main()
{
using int_array_type = int[5];
std::cout << rank<int>::value << std::endl;
std::cout << rank<int[]>::value << std::endl;
std::cout << rank<int[5]>::value << std::endl;
std::cout << rank<int_array_type>::value << std::endl;
std::cout << rank<int_array_type[]>::value << std::endl;
std::cout << rank<int_array_type[5]>::value << std::endl;
return 0;
}
输出结果为:
:g++ -std=c++11 -O0 main.cc -o main
:./main
0
1
1
1
2
2
std::remove_all_extents
#include <iostream>
#include <memory>
#include <type_traits>
#include <typeinfo>
template<class T>
struct remove_all_extents { typedef T type;};
template<class T>
struct remove_all_extents<T[]> {
typedef typename remove_all_extents<T>::type type;
};
template<class T, std::size_t N>
struct remove_all_extents<T[N]> {
typedef typename remove_all_extents<T>::type type;
};
int main()
{
using int_array_type = int[5];
std::cout << typeid(remove_all_extents<int>::type).name() << std::endl;
std::cout << typeid(remove_all_extents<int[]>::type).name() << std::endl;
std::cout << typeid(remove_all_extents<int[5]>::type).name() << std::endl;
std::cout << typeid(remove_all_extents<int_array_type>::type).name() << std::endl;
std::cout << typeid(remove_all_extents<int_array_type[]>::type).name() << std::endl;
std::cout << typeid(remove_all_extents<int_array_type[5]>::type).name() << std::endl;
return 0;
}
输出结果为:
:g++ -std=c++11 -O0 main.cc -o main
:./main | c++filt
int
int
int
int
int
int