There's a strlen, and a wcslen function, but is there a templated character array length function so you can do something like strlen<char>
or strlen<wchar_t>
?
If not, then I guess I'll write my own.
There's a strlen, and a wcslen function, but is there a templated character array length function so you can do something like strlen<char>
or strlen<wchar_t>
?
If not, then I guess I'll write my own.
You have the char_traits helper used by std::string.
It provides char_traits<char>::length
and char_traits<wchar_t>::length
.
If you were using templates wouldn't you be using std::string (which is of course templated) ?
template <class T> size_t strlen( T * _arg )
{
if ( _arg == 0 )
return -1;
size_t i = 0;
while ( _arg[i] != 0 ) ++i;
return i;
}
The simple answer would be std::find, with a special end iterator that never matches anything. (In a template, you're looking for T().)
No.
This is because strlen() and wcslen() are part of C (not C++) and thus handle C-Strings.
C++ discourages the use of C-Strings by providing std::string (std::wstring). These are of course templates of (std::basic_string<T>).
Rather than write your own would it not be better to shift to C++ std::string?
Well the obvious way is to have your function take a basic_string<CharType>
and let the user form one of those. Then all the length stuff is hidden away in the standard library.
If that's not suitable, just keep a running track of the character count as you're copying them into the stream's internal buffer.