简体   繁体   English

Const正确性警告c ++

[英]Const correctness warnings c++

Does anyone know of any warnings that C++ compilers provide that help to enforce const correctness? 有没有人知道C ++编译器提供的任何警告有助于强制执行const正确性? For instance, it would be nice to have a warning produced by any C++ method that contains a non-const parameter that is never modified inside of the method. 例如,任何包含非const参数的C ++方法产生的警告会很好,该参数在方法内部永远不会被修改。 I see that there is a gnu compiler warning called -Wsuggest-attribute=const; 我看到有一个名为-Wsuggest-attribute = const的gnu编译器警告; however, when I use this flag I get an error saying that it is not recognized. 但是,当我使用此标志时,我收到一条错误消息,表示无法识别。 Any ideas why? 有什么想法吗?

I don't think such a warning exists, mostly because it would be useless. 我不认为存在这样的警告,主要是因为它没用。 Just because a parameter is not modified inside the call, doesn't mean it should be made const just for the sake of it. 仅仅因为参数未在调用中被修改,并不意味着它应该仅仅为了它而成为const

Think of virtual functions. 想想virtual功能。 Perhaps the designer of the base class, although not modifying the parameter in the base class, wants to leave it up to an extending class whether or not to modify that parameter. 也许基类的设计者虽然没有修改基类中的参数,但是想要将它留给扩展类,无论是否修改该参数。

Also, think of large applications, where modifying interfaces or API's or whatever costs a lot. 另外,考虑大型应用程序,其中修改接口或API或任何成本很多。 You might not need to modify the parameter now, but intend to do so in the future. 您现在可能不需要修改参数,但将来打算这样做。 You're not going to make it const now, and force a full rebuild and probably risk errors in the future when you remove the const . 你现在不打算将它变成const ,强制进行完全重建,并且当你删除const时,将来可能会冒错误。

Careful, a const parameter like this one: 小心,像这样的const参数:

void myFunc(int const param);

does not belong to the interface. 属于该接口。 It belongs to the local scope of the function implementation . 它属于函数实现的本地范围。 In fact, this function: 实际上,这个功能:

int inc(int const param) { return param+1; }

may be declared as 可以声明为

int inc(int param);

It is not a violation of the const correctness paradigm to claim the right to modify a variable but not actually do it. 声称有权修改变量但实际上没有这样做,这并不违反const正确性范式。

If you are worried about const_cast you can either not use it in the first place or simply grep for it in your code base. 如果你担心const_cast你可以不首先使用它,或者只是在你的代码库中grep它。

-Wsuggest-attribute=const

This analysis requires option 此分析需要选项

-fipa-pure-const

which is enabled by default at 默认情况下启用

-O 

and higher 更高

No, unfortunately there are no such warnings. 不,不幸的是没有这样的警告。 You just get errors if you try to change const declared parameters. 如果您尝试更改const声明的参数,则只会出现错误。 This is because missing const declarations do not change the correctness of the code from the compilers point of view. 这是因为缺少const声明不会从编译器的角度改变代码的正确性。 But const correctness is important for the compiler to discover potential optimizations and it improves the readability of the code. 但const正确性对于编译器发现潜在的优化很重要,并且它提高了代码的可读性。 It is a matter of professionalism. 这是一个专业的问题。 Especially when using references const correctness is a must. 特别是在使用引用时,const必须是正确的。 I often refer to this. 我经常提到这个。
The compiler itself takes const correctness very serious when operators (assignement, conversion, ...) come into play. 当运算符(assignement,conversion,...)发挥作用时,编译器本身会使const正确性非常严重。 A missing const here and the compiler refuses to use the operator because it makes a big difference if the given parameter may possibly be modified or not. 这里缺少const并且编译器拒绝使用运算符,因为如果可能修改给定参数,它会产生很大的不同。

I'm not aware of such warnings and I think they would be rather hard to implement in a compiler - that is, they would slow it down. 我不知道这样的警告,我认为它们在编译器中实现起来相当困难 - 也就是说,它们会减慢它的速度。 Maybe some static analysis tools have such features (but I'm not aware of those either). 也许一些静态分析工具具有这样的功能(但我也不知道这些)。

As per Wsuggest-attribute=const , that is a different thing. 根据Wsuggest-attribute=const ,这是另一回事。 It will suggest to use a gcc-specific "function attribute const ", which is, basically, a mathematical function, receiving only values (no pointers), not reading or changing any static/global state and returning only a value (no pointers). 它将建议使用特定于gcc的“函数属性const ”,它基本上是一个数学函数,只接收值(无指针),不读取或更改任何静态/全局状态并仅返回值(无指针) 。 For further description, look here: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes 有关详细说明,请查看此处: https//gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM