Unsafe Strings
What does the below C++ program do? #include <iostream> #include <string> void someFunction(const std::string& name) { const_cast<std::string&>(name) = "test"; } int main() { const std::string name = "immutable"; std::cout << "before someFunction name = '" << name << "'" << std::endl; someFunction(name); std::cout << "after someFunction name = '" << name << "'" << std::endl; return 0; } name is declared in main as a const variable. We pass name as a constant reference to someFunction assuming it cannot be mutated. But in someFunction we can cast to a mutable reference and write to it. ...