Sell me on const correctness

2019-01-01 05:15发布

So why exactly is it that it's always recommended to use const as often as possible? It seems to me that using const can be more of a pain than a help in C++. But then again, I'm coming at this from the python perspective: if you don't want something to be changed, don't change it. So with that said, here are a few questions:

  1. It seems like every time I mark something as const, I get an error and have to change some other function somewhere to be const too. Then this causes me to have to change another function somewhere else. Is this something that just gets easier with experience?

  2. Are the benefits of using const really enough to compensate for the trouble? If you don't intend on changing an object, why not just not write code that doesn't change it?

I should note that at this point in time, I'm most focused on the benefits of using const for correctness and maintainability purposes, although it is also nice to have an idea of the performance implications.

16条回答
流年柔荑漫光年
2楼-- · 2019-01-01 05:42

const correctness is one of those things that really needs to be in place from the beginning. As you've found, its a big pain to add it on later, especially when there is a lot of dependency between the new functions you are adding and old non-const-correct functions that already exist.

In a lot of the code that I write, its really been worth the effort because we tend to use composition a lot:

class A { ... }
class B { A m_a; const A& getA() const { return m_a; } };

If we did not have const-correctness, then you would have to resort to returning complex objects by value in order to assure yourself that nobody was manipulating class B's internal state behind your back.

In short, const-correctness is a defensive programming mechanism to save yourself from pain down the road.

查看更多
其实,你不懂
3楼-- · 2019-01-01 05:47

Say you have a variable in Python. You know you aren't supposed to modify it. What if you accidentally do?

C++ gives you a way to protect yourself from accidentally doing something you weren't supposed to be able to do in the first place. Technically you can get around it anyways, but you have to put in extra work to shoot yourself.

查看更多
怪性笑人.
4楼-- · 2019-01-01 05:48

const is a promise your are making as a developer, and enlisting the compiler's help in enforcing.

My reasons for being const-correct:

  • It communicates to clients of your function that your will not change the variable or object
  • Accepting arguments by const reference gives you the efficiency of passing by reference with the safety of passing by value.
  • Writing your interfaces as const correct will enable clients to use them. If you write your interface to take in non-const references, clients who are using const will need to cast constness away in order to work with you. This is especially annoying if your interface accepts non-const char*'s, and your clients are using std::strings, since you can only get a const char* from them.
  • Using const will enlist the compiler in keeping you honest so you don't mistakenly change something that shouldn't change.
查看更多
呛了眼睛熬了心
5楼-- · 2019-01-01 05:48

Programming C++ without const is like driving without the safety belt on.

It's a pain to put the safety belt on each time you step in the car, and 364 out of 365 days you'll arrive safely.

The only difference is that when you get in trouble with your car you'll feel it immediately, whereas with programming without const you may have to search for two weeks what caused that crash only to find out that you inadvertently messed up a function argument that you passed by non-const reference for efficiency.

查看更多
低头抚发
6楼-- · 2019-01-01 05:50

Here's a piece of code with a common error that const correctness can protect you against:

void foo(const int DEFCON)
{
   if (DEFCON = 1)     //< FLAGGED AS COMPILER ERROR! WORLD SAVED!
   {
       fire_missiles();
   }
}
查看更多
梦寄多情
7楼-- · 2019-01-01 05:51

My philosophy is that if you're going to use a nit-picky language with compile time checks than make the best use of it you can. const is a compiler enforced way of communicating what you mean... it's better than comments or doxygen will ever be. You're paying the price, why not derive the value?

查看更多
登录 后发表回答