You apparently talk about C++
(no tags yet, so i'll think it is C++ here). Well you seem to want to say
As arguments to my template, i accept any type that models a string
Sadly, currently that is not possible yet. It requires the concept
feature that will be in the next C++ version. Here is a video about them.
What you can do is accept basic_string<CharT, TraitsT>
if you want to keep it generic, for example if you want to accept wide character strings aswell as narrow character strings
template <typename CharT, typename TraitsT>
std::basic_string<CharT, TraitsT> strUpper(basic_string<CharT, TraitsT> theString ) {
typedef basic_string<CharT, TraitsT> StringT;
for(typename StringT::iterator it = theString.begin();
it != theString.end();
++it)
{
*it = std::toupper(*it, std::locale());
}
return theString;
}
It will not accept other or custom string classes that happen to be strings too. If you want that, keep it totally free of what it accepts and what not
template <typename StringT>
StringT strUpper(StringT theString) {
for(typename StringT::iterator it = theString.begin();
it != theString.end();
++it)
{
*it = std::toupper(*it, std::locale());
}
return theString;
}
You will have to tell the users of that library what functions and type they have to expose for that to work. The compiler will not know about that contract. Instead, it will just throw error messages when it comes to call the function with a type that's not a string. Often, you will find pages of error messages and it's difficult to get to the real reason of what goes wrong. The proposed Concepts feature for the next version of the standard fixes that nicely.
If you did not intend to write such a generic function, you can just go ahead and write a normal function like this
std::string strUpper(std::string theString) {
for(std::string::iterator it = theString.begin();
it != theString.end();
++it)
{
*it = std::toupper(*it, std::locale());
}
return theString;
}
If you did not intend to invent that function to learn in the first place, but because you did not find another already written algorithm, look into the boost string algorithm library. Then you can write
boost::algorithm::to_upper(mystring);