Building a const-correct library (C ++), which relies on a non-constant-correct library

I am creating a C ++ library (mainly for fun) that I have been working on for some time (years, haha, it's just a hobby)

I recently switched some reinforcements (reading, library dependency) to another library. Unfortunately, the library mentioned is not related to const-correctness at all. I'm a bit of an OCD and I like to challenge myself to do things the β€œRight Way”, so I want my library to be const-correct. I started this several times, and some parts; I know that it is better to start const-correct from the very beginning, but this is not very relevant or for discussion. The fact is that I am interested in giving it a serious continuation, but another library prevents me from doing this.

You know, you can ask?

Well, if I call a method that explicitly needs to be const (actually doesn't change anything), and my method is also a candidate for consting (new word), I cannot provide constness on my method until this other method also const .

Example:

// Bad third-party library struct Foo { void should_be_const() {} }; // My library struct Bar { Foo my_foo; void should_be_const() const { my_foo.should_be_const(); // ERROR! Not a const function! } }; 

This is only apparent from empirical evidence and from my research and understanding of constness. However, this may be a falsely shaped understanding, so I am open (and hope?) So that it is incorrect and shows differently (although this would shake the basis of my understanding of the consistency haha)

What poor soul should do if he (or she) wants to write the right library, but the dependency is not written that way?

Hope this is a suitable question for SO. Please (I'm sure you will do this) let me know if this is not the case, or if there is a better StackExchange site to publish it.

PS I found this SO question , but I was hoping that a theme / solution could be developed.

+7
c ++
source share
1 answer

Another question you refer to gives a pretty good idea of ​​the problems, but I do not agree with the answer. The interfaces in your sub-libraries will be divided into four cases.

  • Things that are not constants and cannot be expected.
  • Strings declared as const
  • That which should logically be const, and

    3a. Performed as such, but not declared as such

    3b. Not implemented as such

Case 1 is simple - if you want yours to be const, you need to make a copy of the input before calling this interface. The throw will be simply wrong and will ultimately lead to a failure or other error. Case 2 is not a problem. So, cases 3 are so.

Unfortunately, the reality is that cases 3a and 3b should be treated the same in case 1, because they are indistinguishable. Implementation may vary by platform or version. So you have to create a copy and then call the interface. Notice, I said "must." The reality is that most of us will use const_cast to invoke interfaces that we think we understand (e.g. strcmp). This becomes a judgment on how confident we are in implementation. Personally, I would not worry about strcmp. Almost any higher level can use something like strtok, which will break things.

+2
source share

All Articles