std::remove_extent返回數組降低一個維度後的數據類型, std::rank返回數組維度, std::extent返回數組第一個維度的長度

std::remove_extent
 返回數組降低一個維度後的數據類型。不改變數據類型的限制屬性(const, volatile, const volatile)
 一維數組降低到0維度;
 二維數組降低到一維數組;
 三維數組降低到二維數組;

std::extent
 返回數組第一個維度的長度

std::remove_all_extents
 返回數組的實際類型, int[] -> int, int[][2] -> int, int[][3][4] -> int

    int arrO[] = { 1,2,3,4 };
	int arrT[2][3] = { {1,2,3},{3,4,5} };
	std::remove_extent<decltype(arrO)>::type oa{}; // int
	std::remove_extent<decltype(arrT)>::type ob{}; // int[3]
	cout << std::extent<decltype(arrO)>::value << endl; // 4,  一維數組的長度
	cout << std::extent<decltype(arrT)>::value << endl; // 2 , 二維數組的第一維度的長度

	const int arrF[3] = {};
	const int arrS[4][3] = { {1,2,3},{4,5,6} };
	std::remove_extent<decltype(arrF)>::type oc{}; // const int
	std::remove_extent<decltype(arrS)>::type od{}; // const int[3]
	cout << std::extent<decltype(arrF)>::value << endl; // 3
	cout << std::extent<decltype(arrS)>::value << endl; // 4

	volatile int arrD[] = { 1,2 };
	volatile int arrE[][2] = { {1,2},{3,4}, { 5,6 } };
	std::remove_extent<decltype(arrD)>::type oe{}; // volatile int
	std::remove_extent<decltype(arrE)>::type of{}; // volatile int[2]
	cout << std::extent<decltype(arrD)>::value << endl; // 2
	cout << std::extent<decltype(arrE)>::value << endl; // 3

	const volatile int arrQ[] = { 1,2 };
	const volatile int arrP[][3] = { {1,2,3},{4,5,6} };
	std::remove_extent<decltype(arrQ)>::type og{}; // const volatile int
	std::remove_extent<decltype(arrP)>::type oh{}; // const volatile int[3]
	cout << std::extent<decltype(arrQ)>::value << endl; // 2
	cout << std::extent<decltype(arrP)>::value << endl; // 2

std::rank

返回數組的維度

int arr[] = { 1,2 };
int arrA[][3] = { {1,2,3} };
int arrB[1][2][3] = {};
std::rank<decltype(arr)>::value << endl;  // 1, 一維數組
std::rank<decltype(arrA)>::value << endl; // 2 , 二維數組
std::rank<decltype(arrB)>::value << endl; // 3 , 三維數組
template<class _Ty, _Ty _Val>
struct integral_constant
{
	static constexpr _Ty value = _Val;

	using value_type = _Ty;
	using type = integral_constant;
    
    // intergral_constant可以像int一樣使用
	constexpr operator value_type() const noexcept
	{
		return (value);
	}
    
    // intergral_constant可以像int一樣使用
	constexpr value_type operator()() const noexcept
	{
		return (value);
	}
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章