[英]conversion to static_cast<unsigned char>
Does a conversion like: 转换是否像:
int a[3];
char i=1;
a[ static_cast<unsigned char>(i) ];
introduce any overhead like conversions or can the compiler optimize everything away? 引入任何开销,如转换或编译器可以优化一切? I am interested because I want to get rid of
-Wchar-subscripts
warnings, but want to use a char as index (other reasons) 我感兴趣因为我想摆脱
-Wchar-subscripts
警告,但想使用char作为索引(其他原因)
I did one test on Clang 3.4.1 for this code : 我在这段代码上对Clang 3.4.1进行了一次测试:
int ival(signed char c) {
int a[] = {0,1,2,3,4,5,6,7,8,9};
unsigned char u = static_cast<unsigned char>(c);
return a[u];
}
Here is the relevant part or the assembly file generated with c++ -S -O3
这是使用
c++ -S -O3
生成的相关部分或汇编文件
_Z4ivala: # @_Z4ivala
# BB#0:
pushl %ebp
movl %esp, %ebp
movzbl 8(%ebp), %eax
movl .L_ZZ4ivalaE1a(,%eax,4), %eax
popl %ebp
ret
There is no trace of the conversion. 没有转换的痕迹。
在大多数现代建筑char
和unsigned char
具有相同的大小和排列,因此unsigned char
可以代表所有非负值char
和铸造彼此不需要任何CPU指令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.