You could write a function template back
that delegates to the member function for ordinary containers and a normal function that implements the missing functionality for strings:
template <typename C>
typename C::reference back(C& container)
{
return container.back();
}
template <typename C>
typename C::const_reference back(const C& container)
{
return container.back();
}
char& back(std::string& str)
{
return *(str.end() - 1);
}
char back(const std::string& str)
{
return *(str.end() - 1);
}
Then you can just say back(foo)
without worrying whether foo
is a string or a vector.