如何找到应该是const的C ++函数? [英] How can I find C++ functions that should be const?

查看:157
本文介绍了如何找到应该是const的C ++函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

#include <stdio.h>

class A
{
public:
  int doit()
  {
    return 5;
  }
  int doit2() const
  {
    i++;
    return i;
  }
  int i;
};

int main()
{
  A a;
  printf("%d\n", a.doit() );
  return 0;
}

其中使用g ++ -Wall -Wpedantic main.cpp进行编译。有没有办法让g ++说A :: doit()应该标记为const? g ++ 4.8有-Wsuggest-attribute = const,但是在这种情况下似乎不工作。 g ++ -Wall -Wpedantic -Wsuggest-attribute = const const_main.cpp -fipa-pure-const -O2 -Wextra仍然是干净的。

Which compiles cleanly with g++ -Wall -Wpedantic main.cpp. Is there some way to get g++ to say "A::doit() should be marked as const"? g++ 4.8 has -Wsuggest-attribute=const but it doesn't seem to work in this case. g++ -Wall -Wpedantic -Wsuggest-attribute=const const_main.cpp -fipa-pure-const -O2 -Wextra is still clean.

我同意const是一个设计决定,但我正在处理的是一个案例的许多行的遗留代码,并为新的开发人员来到它将是有益的,如果const函数被标记为这样。我认为编译器知道足够,因为如果我将一个函数标记为const然后修改状态将抛出一个错误。我只是要求相反的,所以我可以通过和标记const函数作为const和我甚至不需要它是完美的,找出真正复杂的情况,我会解决的简单的情况,因为我有

I agree that const is a design decision but what I'm dealing with is a case of many lines of legacy code and for new developers coming in it would be helpful if const functions were marked as such. I think the compiler knows enough because if I mark a function as const and then modify state it will throw an error. I'm just asking for the opposite of that so that I can rip through and mark const functions as const and I don't even need it to be perfect and figure out really complicated cases, I would settle for the simple cases as I have outlined in the code above.

现在我添加了一个非const函数doit2(),但是声明了它的const,编译器说:

Now I added a non-const function doit2() but declared it const and the compiler says:

const_main.cpp: In member function ‘int A::doit2() const’:
const_main.cpp:12:6: error: increment of member ‘A::i’ in read-only object
     i++;
      ^

我只需要相反的(告诉我,

I just need the opposite of that ( tell me when it could be const but it isn't marked as such ).

找到了这里的答案: Const正确性警告c ++

推荐答案

否。设计接口不仅仅是为了允许当前的行为 - 它也是关于保留自由和表达限制,所以客户端代码不会由于类的代码的未来更改打破。 gcc要期望 doit() const ,因为代码的一些预期或设想的演变可能需要它是非 - const ...你负责表达这个设计决策。测试用例可以帮助确保预期的操作,并且只允许预期的操作,但是测试用例将反映您的关于客户端使用的设计决策,而不是编译器决定的内容。

No. Designing an interface is not just about allowing current behaviour - it's also about reserving freedoms and expressing limitations so the client code isn't broken by future changes to your class's code. It's not reasonable for gcc to expect that doit() should be const, as some intended or envisaged evolution of the code may require it to be non-const... you are responsible for expressing this design decision. Test cases can help to ensure intended and only intended operations are allowed, but again the test cases will be a reflection of your design decision about client usage, and not something decided by the compiler.

一个非常简单的例子是一个初始实现的函数ala throw Not_Implemented(); - 表示不需要是非 - const ,但是如果逻辑操作的函数请求最终被实现,并且需要更改observable对象状态,那么它不应该最初标记为 const std :: string :: shrink_to_fit()是一个类似的例子 - 它可能是空的,因此在未实现时可能会 const (标准说它是一个非绑定请求),但应该是非 const ,因为当实现它改变对象的可观察状态(例如未来的迭代器无效在行动期间现在可以触发较早的 resize())。

A very simple example is a function with an initial implementation ala throw Not_Implemented(); - the throw suggests no need to be non-const, but if the logical operation the function requests is eventually implemented and would need to change the observable object state, then it should be not be initially marked const. std::string::shrink_to_fit() is a similar example - it might be empty and therefore potentially const when unimplemented (the Standard says it's a non-binding request), but should be non-const because when implemented it changes the observable state of the object (e.g. future iterator invalidation during actions that may now trigger an earlier resize()).

这篇关于如何找到应该是const的C ++函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆