The whole idea with "short string optimisation" is that it "takes no extra space. So the size is calculated such that the local buffer overlays other variables in the class that are used when the string is longer.
It is a bad idea to modify system headers, since they are often compiler version dependent, and there are implementation details that make it "binary incompatible".
As the comment says, make sure this really is a problem (performance or otherwise) before doing anything about it. And then consider carefully what you should do about it. What problem are you trying to fix, and are you sure it's worth it. Remember that if you do something like:
std::string func(std::string arg)
{
...
}
you will copy more bytes on passing arg
on the stack. And no, it doesn't really help making it const std::string& arg
if your calling code makes a temporary string, e.g. func("Name: " + name);
. And if you do vector<std::string>
, the size of each will be bigger, so the vector will take more space - even for the cases where the string STILL don't fit, so more time will be taken when you grow/shrink the vector.
And I think the right solution, once you have made a decision, is to implement your own string class. std::string
is a standard template library class, they are not extendable, and you aren't supposed to modify the standard library header files, as, like I said earlier, it's highly compiler dependent. It will be quite some work to make it completely compatible with std::string
, but you could of course "cheat" and make a converter function operator std::string()
for your string class, so you only need to produce the more basic functions that std::string
offers.