2

我维护了一个开源程序,我的一个用户报告说它不会在clang我以前从未使用过的 . 我得到的错误之一是*Warning: qualifier on function type 'junky_t' (aka 'void (const int &, const int &)') has unspecified behavior.*。我创建了一个演示该问题的小程序:

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 引用。const类中的demo应该防止junk被更改。但是显然它是模棱两可的,因为可能会考虑函数本身const,但事实并非如此。我用gccor编译它没有问题,llvm但它不会clang在 Mac 上编译。我应该怎么办?

4

1 回答 1

3

这不是未指明的行为。clang的警告是错误的。您的代码是合法的 c++。标准 (C++11) 规定,函数类型之上的 cv 限定符被忽略。

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

junky_t * const junk;

否则就写

junky_t * junk;

于 2012-11-22T15:15:59.363 回答