且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 Visual C++ 在 C 中对从 const void ** 到 void * 的隐式转换发出警告,但在 C++ 中却没有?

更新时间:2023-11-08 18:15:10

显然这只是VC++中的一个bug.

Apparently this is simply a bug in VC++.

如果你声明 const char **x; 结果是一个指向 chars 的只读"指针的指针,它本身不是一个只读"指针(我使用术语只读",因为 const-ness 术语提出了错误的概念,即所指向的字符是常量,而这通常是错误的...... const 带有引用和指针是引用或指针的一个属性,它不说明指向或引用数据的常量性.

If you declare const char **x; the result is a pointer to a "read-only" pointer to chars, and it's not itself a "read-only" pointer (I use the term "read-only" because const-ness term pushes the wrong concept that the character being pointed to is constant while this is false in general... const with references and pointers is a property of the reference or of the pointer and tells nothing about constness of the pointed-to or referenced data).

任何读/写指针都可以转换为 void * 并且 VC++ 在编译该代码时没有真正的理由发出警告,无论是在 C 还是在 C++模式.

Any read/write pointer can be converted to a void * and VC++ has no real reason to emit a warning when compiling that code, neither in C nor in C++ mode.

请注意,这不是正式的问题,因为该标准没有规定应该或不应该发出哪些警告,因此编译器可以***地为仍然保持合规的完全有效的代码发出警告.VC++ 实际上会针对有效的 C++ 代码发出大量警告...

Note that this is not formally a problem because the standard doesn't mandate which warnings should or should not be issued and therefore a compiler is free to emit warnings for perfectly valid code still remaining compliant. VC++ actually emits a plethora of those warnings for valid C++ code...