为什么这个 const 说明符具有未指定的行为

Why does this const specifier have unspecified behavior?

本文关键字:未指定 const 说明符 为什么      更新时间:2023-10-16

我维护一个开源程序,我的一个用户报告说它不会在我以前从未使用过的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,但事实并非如此。我用gccllvm编译它没有问题,但它不会在 Mac 上使用 clang 编译。我该怎么办?

这不是未指定的行为。叮当的警告是错误的。你的代码是合法的 c++。标准 (C++11) 指出,函数类型顶部的 cv 限定符将被忽略。

因此,将 const 限定符放在函数类型之上没有任何意义。如果你希望你的指针是常量,那么写

junky_t * const junk;

否则就写

junky_t * junk;