I hope I’ve titled my question correctly.
I have a very large/old database with many non-null terminated char arrays and a few std::string types. I’m creating a tool that will use this database, and I’d like to ensure it’s safety with respect to the non-null terminated char arrays. Ideally I’d like to support both types to make it easy for the developer to use the tool.
void AddCell( const std::string & _cell_str );
void AddCell( const char * _cell_data, size_t _array_size );
However, there’s a specific scenario that could be problematic: if a new developer is unaware that the char array is non-null terminated and out of laziness decides to use the implicit conversion of the std::string because he doesn’t have to enter the array size, then it’s possible that the std::string will show some unwanted chars.
How can I deal with this problem? Should I force someone to specify the length of the std::string? Should I just remove the std::string overloaded function and make everyone use c_str() from their std::string? Ideally, I’d just use the explicit keyword, but it’s only available for constructors. I’m hesitant to steer away from the use of std::string.
You can change the first overload so that instead of taking a std::string reference it takes a reference to a new type that you create that has an implicit conversion from std::string. So you won’t see the implicit conversion from const char*, but will take std::string with no problems.