如何获取数组的数据类型的大小?例如 int32_t arr[10][10]
datatype is int32_t
and it's size is 4 bytes
,类似地,char [10][100]
datatype is char
and it's size is 1 byte
。我问这个问题的原因是,在通过网络发送数据之前,我需要知道是否必须交换数据,因为大小大于1
需要交换的数据类型和大小等于的数据类型1
不应该交换(实际上是交换没有任何意义)。
int32_t int_arr[10][10][10]; // execution should go to if part
if (sizeof(decltype(int_arr)) > 1) // this doesn't work for array type
// swap each element and then copy to send buff
else
// don't swap, simply copy to send buff
char char_arr[10][10]; // execution should go to else part
if (sizeof(decltype(char_arr)) > 1) // this doesn't work for array type
// swap each element and then copy to send buff
else
// don't swap, simply copy to send buff
您可能正在寻找
sizeof(std::remove_all_extents_t<decltype(int_arr)>)
也适用于非数组;当给定非数组类型时T
,std::remove_all_extents_t<T>
为T
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句