- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
考虑以下代码,这些代码来自 this question :
const int (*foo(const char *a))[1]
{ return (const int (*)[1]) a; }
当 compiled with GCC 8.2 (and older versions) using -Wcast-qual
, GCC 警告:
source>:2:15: warning: cast discards 'const' qualifier from pointer target type [-Wcast-qual] { return (const int (*)[1]) a; } ^
Is this warning correct? Clearly the destination type has a const
qualifier in it.
It is on the element type rather than on the thing immediately pointed to by the pointer, which is the array type. However, the warning remains even if we use typedef int T[1];
and replace the cast with (const T *)
. Additionally, per C 2018 6.7.3 10, qualifiers on an array type apply to the element type, not the array type, so the type is the same either way.
Clang does not show this warning.
If we change the cast to (const void *)
:
const int (*foo(const char *a))[1]
{ return (const void *) a; }
然后警告消失了。如果我们将 -pedantic
添加到编译开关,我们会收到关于 const
的不同警告:
source>:2:15: warning: return discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers] { return (const void *) a; } ^~~~~~~~~~~~~~~~
这看起来像同一个警告,只是它是关于从返回表达式到函数返回类型的隐式转换,而之前的警告是关于强制转换中的显式转换。但是这个只出现在 -pedantic
中。为什么?
最佳答案
这是 GCC bug 81631 . GCC 无法识别指向数组指针的转换保留了 const 限定符,因为应用于数组的限定符实际上应用于数组元素。
关于c - GCC 关于 const 限定符的警告是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54507856/
我一直在使用 OpenGL 编写程序。最近,我开始学习OpenGL着色语言。我是新手;因此,请在回答中详细说明。 我的问题是: GLSL 中有哪些不同类型的变量(限定符)? 它们的用途是什么? 它们有
这个问题在这里已经有了答案: typedef pointer const weirdness (6 个答案) 关闭 8 年前。 我有一个结构体 type_s。然后我将指向 struct type_s
我有一个 Point2D 类如下: class Point2D{ int x; int y; public: Point2D(int inX,
当应用于指向指针的指针等时,我在推断什么是 const 时遇到了一些麻烦。即,什么是 const 当你有 const Foo **foo; 我可以在这里更改 **foo 中的内容吗?如 foo[0]
限定符 有时候不知道要匹配多少字符。为了能适应这种不确定性,正则表达式支持限定符的概念。这些限定符可以指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。 下表给出了各种限定符及其含义的说
我有一个包含字符名称的数组结构。我想使用 qsort 按字母顺序对它们进行排序,但是我一直收到一条错误消息,说“初始化丢弃了指针目标类型中的‘const’限定符”。我相信我的 cmpmi() 函数和
我是 Java EE/JSF 的新手,现在阅读 CDI 限定符 - 更改类实现的可能性。这很好,但我有一个问题。据我了解,我可以使用限定符更改类实现,但我需要在使用此实现的任何地方更改它。在一个地方进
假设我有以下目录结构: $ mkdir -p a/1 $ ln -s a b 查找目录,我也得到了符号链接(symbolic link)中的目录: $ print -l */*(/) a/1 b/1
我正在尝试创建一个 const 结构数组,但我不断得到 error initializer element is not a compile time constant 我使用的是keil IDE。这
#include using namespace std; class Point { private: int x, y; // Private data members public:
我想使一个方法有条件地成为常量。也就是说,我们有 template class A { // stuff }; 根据模板参数的值,要么 void method() const; 或 void
由于 const int 特化导致以下错误: #include using std::cout; using std::endl; template void g(T val) { cou
同时适用于移动设备和平板电脑的 Titanium 项目,完美地完成了移动版本,但在平板设计时面临一些复杂性。 下面是 Titanium 支持的限定符矩阵,但我找不到任何针对 Android-Table
我正在研究 C++ 和 gtkmm,试图创建一个自定义类,它调用 .glade 文件来构建一个 Gtk 而不是基本的界面。此类派生自 Gtk::Window,但是当我调用类本身的 get_widget
我编写了以下代码来测试 const 成员函数:当我有数据成员的 const 限定符时,它编译并运行良好char *data 和构造函数的参数。但是,如果我从中删除 const数据成员和构造函数,我得到
您好,我正在使用 C++ 内置算法。我这里有这段代码: #include #include #include using namespace std; bool isDon(string& na
我的问题与 Time 项目有关,我将在下面完整发布该项目以提供背景信息。我所做的大部分都是正确的,但是当涉及到加法和减法运算符定义时,我遇到了范围界定错误的问题。编译器找不到我的私有(private)
我有以下代码- int acb(const uint16 *MsgPtr) { uint16 *p = (MsgPtr + 1); printf("%d", *p); } 我收到以下警告 - 在为 p
我在结构中有一个指向结构实例链表的指针字段。 struct myStruct { some fields... struct list_objects * volatile list;
所以我在维基百科的一篇文章(粗略翻译)中遇到了以下定义: Modifier (programming) - element of source code being a phrase of given
我是一名优秀的程序员,十分优秀!