What's wrong with const?

前端 未结 12 2026
無奈伤痛
無奈伤痛 2021-01-31 12:06

What are the known shortfalls of const in C++ and C++0x?

相关标签:
12条回答
  • 2021-01-31 12:43

    The problem with const is the programmers that use it incorrectly our inconsistently

    0 讨论(0)
  • 2021-01-31 12:45

    What's wrong with const is that many programmers don't seem to be able to understand it completely, and a "half-const-correct" project simply does not work. This is what you need to know:

    1. Foo vs. const Foo (or Foo const)
    2. Foo& vs. const Foo& (or Foo const&)
      • references-to-const bind to all kinds of things, while references-to-non-const don't
    3. Foo* vs. const Foo* (or Foo const*)
      • pointer variables can also be Foo* const and const Foo* const (or Foo const* const)
    4. void Foo::mutator() vs. int Foo::accessor() const
      • but pointer members inside const member functions still point to non-const objects
      • so we can accidentally return non-const data from a const-function
    5. iterator vs. const_iterator
      • iterator variables can also be const iterator and const const_iterator

    Migrating to C++ from a language that has no const concept is quite hard, any many fail to see the point.

    0 讨论(0)
  • 2021-01-31 12:47

    const is great. const is important. const-correctness is necessary condition for an API to be good.

    Yet there are two issue I've had with const, repeatedly.

    • There's no way to mark a variable as const retroactively. You must either declare a variable code, in which case you have to initialize it immediatly. What if the initialization code contains an if, though? You have the choice of either omitting the const (undesirably), using operator ? instead of if (harms readability). Java get's that right, BTW - const variables don't have to be initialized right away, they just have to be initialized before they're first read, and they have to be initialized in all branches of an if.

    • There's no way to specifiy that an object passed by reference to a function won't change for the duration of the function call. const T& t does not mean that the object pointed to by t won't change, but only that the reference t cannot be used to change it. The compiller still has to assume that any function call which it does not see into might change the object. It some cases, that prevents quite a few optimizations.

    0 讨论(0)
  • 2021-01-31 12:51

    The two main issues that I have seen frequent complaints about in newsgroups, are

    • The need to waste a lot of time on supporting non-const-aware APIs (especially Microsoft's).

    • The need to define both const and non-const version of a method.

    I think the latter could/should be supported by the language.

    Possibly in conjunction with support for covariant member function implementations, because both need some way to pick up the type of the this pointer.

    A third issue is that

    • const does not propagate to owned objects.

    Cheers & hth.,

    0 讨论(0)
  • 2021-01-31 12:52

    One problem is that the language also lets you const_cast it away, which defeats the purpose of using const in the first place.

    0 讨论(0)
  • 2021-01-31 12:53

    Most of the answers below state things such as "what is wrong with const is that X people do Y". Those are not answers but symptoms. Those are not things wrong with const. There's barely any wrong thing with const... Those are things wrong with people who can't RTFM.

    0 讨论(0)
提交回复
热议问题