There is one caveat to be aware of: if substr is called with a position past the end of the array (superior to the size), then an out_of_range
exception is thrown.
Therefore:
std::string tail(std::string const& source, size_t const length) {
if (length >= source.size()) { return source; }
return source.substr(source.size() - length);
} // tail
You can use it as:
std::string t = tail(source, 6);