为什么这个 const 说明符具有未指定的行为
Why does this const specifier have unspecified behavior?
我维护一个开源程序,我的一个用户报告说它不会在我以前从未使用过的clang
下编译。我收到的错误之一是*警告:函数类型"junky_t"(又名"void(const int &,const int &)")上的限定符具有未指定的行为。我创建了一个小程序来演示这个问题:
typedef void junky_t(const int &foo,const int &bar);
class demo {
public:;
const junky_t *junk;
};
这是我尝试编译时发生的情况:
$clang -DHAVE_CONFIG_H -g -g -O3 -Wall -MD -Wpointer-arith -Wshadow -Wwrite-strings -Wcast-align -Wredundant-decls -Wdisabled-optimization -Wfloat-equal -Wmultichar -Wmissing-noreturn -Woverloaded-virtual -Wsign-promo -funit-at-a-time -Weffc++ -D_FORTIFY_SOURCE=2 -MT demo.o -MD -MP -c -o demo.o demo.cpp
demo.cpp:5:5: warning: qualifier on function type 'junky_t' (aka 'void (const int &, const int &)') has unspecified behavior
const junky_t *junk;
^~~~~~~~~~~~~
1 warning generated.
也就是说,类 demo
有一个函数指针,指向一个函数,该函数的签名接受许多 const 引用。类demo
中的const
应该防止junk
被更改。然而,显然它是模棱两可的,因为函数本身可能被认为是const
,但事实并非如此。我用gcc
或llvm
编译它没有问题,但它不会在 Mac 上使用 clang
编译。我该怎么办?
这不是未指定的行为。叮当的警告是错误的。你的代码是合法的 c++。标准 (C++11) 指出,函数类型顶部的 cv 限定符将被忽略。
因此,将 const 限定符放在函数类型之上没有任何意义。如果你希望你的指针是常量,那么写
junky_t * const junk;
否则就写
junky_t * junk;
相关文章:
- 从 XML 中读取未指定结构的每个数据成员
- 用数据填充未指定大小的数组
- 访问从联合与另一个成员集复制的联合中的一个成员是否未定义或未指定?
- 双循环变量的相等条件:未指定还是未定义
- SQL Server-未找到数据源名称,也未指定默认驱动程序
- 无法创建长度未指定
- 在Visual Studio中更新Qt翻译(.ts文件)时出现未指定错误
- 是否未指定在未评估的上下文中实例化模板/lambda
- 未定义与未指定与实现定义的行为
- 在 c++ 中获取未指定数量的输入
- 摆动,传递未指定维度的阵列
- 在尝试提交作业警告时,我一直在警告:与未指定行为的字符串字面结果比较[-WADDRESS]
- 我的基本C 程序中有一个未指定的错误
- OPENCV错误:未指定的错误(未实现该函数)
- 函数参数中数组大小未指定
- 从源代码编译 openCV:*** 未指定目标,也未找到生成文件.停
- 为什么typeID().名称(),c 在输出中未显示const
- 超载构造函数中的未指定字段
- CPack NSIS 组件未指定
- 为什么这个 const 说明符具有未指定的行为