带有 const char* 数组的数组初始化的 C4838 警告 [英] C4838 warning with array initialization of a const char* array

查看:79
本文介绍了带有 const char* 数组的数组初始化的 C4838 警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我将着色器编译为二进制数据,然后将它们转换为头文件,该文件将着色器的字节码声明为 const char 数组.

For a project I compile my shaders to binary data and then convert them to a header file that declares the byte code of the shader as a const char array.

但是,在编译期间我收到以下警告:

However, during compilation I get the following warning:

"从 intconst char 的转换需要缩小转换".

"conversion from int to const char requires a narrowing conversion".

现在我通常知道为什么会发生这种情况,因为编译器认为这些值是 int 类型,而实际上它们被声明为 char 类型.

Now I normally know why this happens, it's because the compiler thinks the values are of type int, while they are in fact declared as a type of char.

const char base_ps[]={
0x44,0x58,0x42,0x43,0x12 ... etc

当鼠标悬停在这些值上时,IntelliSense 还指出这些值被转换为 int.例如,对于索引 0 处的值,它列出了 (int)0x44.

When hovering over these values IntelliSense also points out these are casted to an int. For instance, for the value at index 0 it lists (int)0x44.

有什么办法可以解决这个问题吗?显然我可以在每个值之前明确地将 (char) 放在任何地方,但是我必须在着色器生成中添加一个额外的步骤来解析头文件.

Is there any work around for this? Obviously I can explicity put (char) everywhere before each value, however I'd have to add an extra step into shader generation for this that parses the header file.

我也不想抑制警告,但我希望将警告视为错误.对此有什么建议吗?

I kind of don't want to suppress the warning either, yet I want warnings treated as errors. Any advice on this?

推荐答案

问题是因为您实际上使用的十六进制值太大而无法存储在 char(通常有符号和 8位长).所以最大的正值是 127,或者 0x7F.您提供的值就像您在此处显示的一样包括超过 127 的值,因此您会收到正确的缩小转换警告.

The issue is because you are actually using hex values that are too large to store in a char (which is usually signed and 8 bits long). So the largest positive value is 127, or 0x7F. The values you are providing like you show here include values over 127, hence you get a correct narrowing conversion warning.

要么使用 unsigned char 而不是 char,或者使用较小的数字(不大于 0x7F)像这里:

Either use an unsigned char instead of char, or use smaller numbers (not larger than 0x7F) like here:

const char deferred_ps[]={
    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 
    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
    0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
    0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
    0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
    0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
    0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
    0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
};

请注意,如果您忽略缩小转换警告,则结果是实现定义的,这是不可移植的.

Note that if you ignore the narrowing conversion warning, the result is implementation-defined, which is non-portable.

这篇关于带有 const char* 数组的数组初始化的 C4838 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆