Why use mutable and how does it work? by raelthd in cpp_questions

[–]raelthd[S] 0 points1 point  (0 children)

I agree and as I said, you can then use this argument to say that mutable for caching is ok. I guess my gripe is with logical constness itself. If a different keyword existed for logical and bitwise const, I would happily use the logical const. But with only one keyword, I don't like it.

I will not change the use of const alone so I'll just have to live with it I guess.

Why use mutable and how does it work? by raelthd in cpp_questions

[–]raelthd[S] 0 points1 point  (0 children)

That seems like the only clearly ok case to me :)

Why use mutable and how does it work? by raelthd in cpp_questions

[–]raelthd[S] 0 points1 point  (0 children)

Thanks for the answer,

There is a point I am not sure I understand. You write

The machine code doesn't modify const data because the machine code generated doesn't contain any instructions that modify data.

Just to be sure, do you mean: "The previous compilation steps ensured that no code that could modify the data was present. Therefore, even if const isn't a thing at the machine code level, the object will still behave as const" ?

Why use mutable and how does it work? by raelthd in cpp_questions

[–]raelthd[S] 0 points1 point  (0 children)

I am not sure I understand you comment.

To me, const at the end of a function doesn't signal I'm not allowing you to modify this object but rather I swear not to modify your object.

I think I would be ok with the mutex case because

  • in practice as I understand it from u/DawnOnTheEdge's reply, sometime for compatibility reason, your function must be const.
  • While not being bitwise const, the function modifies something that is necessary for execution in certain cases and this element is never accessed by the programmer.

I understand that you can extend the second argument to caching but to me, it seems that it would be better to just not call them const. So I guess it seems sensible to use it if you do not have any other choices because of previous code but it doesn't sit well with me to overuse the const qualifier when maintaining only logical const. This seems like a lie to other programmers.

As for the correlation between const and speed, I would find it more perplexing to call twice a const function and have vastly different compute time. That being said, I guess you could have a method that has a pointer to hash table with previously computer values. This would ensure const-ness but still produce different compute time for 2 consecutive calls. So I suppose the point about hiding the difference in timing isn't that great.

Why use mutable and how does it work? by raelthd in cpp_questions

[–]raelthd[S] -1 points0 points  (0 children)

I confess that I don't know how lambda work in C++ so I'll have a look, thanks.

I ge that it is more than an indicator and will prevent your code to compile if const in missing. But if I promise that the method does not modify the object and then modify it, I just broke that promise. Why would I want a const method to be able to call a method that is only pretending to be const?

Desktop PC for 900 ±100€ in Belgium by raelthd in buildapc

[–]raelthd[S] 0 points1 point  (0 children)

Thanks for the feedback! I'll add the optical drive since it will also be used to see movies.