(C++) Exercise \#8: library trouble
August 7, 2019 · View on GitHub
(C++) 
Exercise #8: library trouble
Difficulty: 2/10
Date added: 31th of May 2009
This exercise is only for those that use the C++ Builder 6.0 library (otherwise you will not run into the same trouble).
In this exercise, you learn to overcome trouble from your library and an unexpected difference between static_cast and const_cast.
Any programmer might want to write code like below:
const std::string s = "abc***def"; const int n = 3; //Number of repeats std::search_n( s.begin(),s.end(),n,'*'); //Search for three successive asterisks
Programmers (like me) that use the C++ Builder 6.0 Enterprise edition IDE with the Borland compiler BCC32.EXE (version 6.0.10.157) will be in for a surprise: an unexpected compile error!
The compile error given is 'Cannot modify const object', because 'n' is of type 'const int'.
Question #0
Why doesn't this compile?
View the answer of this exercise
Question #1
It is generally a bad idea to modify standard header files. Assume you do not want to do this.
Which workarounds can you find to solve this problem?
Which one is best?
View the answer of this exercise