I have a function with the following signature:
std::string f(const char *first, const char *last) {
std::string result;
std::for_each(first, last, some_lambda_which_appends_to_result);
return result;
}
and overloading for std :: string, which calls this:
std::string f(const std::string s) {
return f(&*s.begin(), &*s.end());
}
However, this may be unsafe (dereferencing s.end () may be a violation of the red card in itself). Is there a safe way to get a pointer to the beginning of characters and a one-by-one pointer (two null pointers would be good in case of an empty string), or do I need to write
std::string(const std::string& s) {
return s.empty() ? std::string() : f(& s.front(), & s.front() + s.size());
}
source
share