[英]How to get the size of datatype of an array
如何获取数组数据类型的大小? 例如。 int32_t arr[10][10]
数据类型是int32_t
并且它的大小是4 bytes
,同样对于char [10][100]
数据类型是char
并且它的大小是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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.