19

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.

leetNightshade
  • 2,673
  • 2
  • 36
  • 47
  • if you are working under Windows, there is the whole lot of _tcs* functions, but it's something different (and in 2011 you shouldn't still program for the Ansi charset in Windows) – xanatos Mar 31 '11 at 17:48
  • 1
    Is there any reason why you don't just write your own ambiguous overload for this? – hiddensunset4 Apr 01 '11 at 04:25
  • 1
    @Daniel Turns out I don't need to write an overload, just look at the answer below. I just wanted to use the standard if it existed, and in the process I found out about the char_traits features; if I went with what you said I wouldn't have learned anything. – leetNightshade Apr 02 '11 at 02:49

6 Answers6

25

You have the char_traits helper used by std::string.

It provides char_traits<char>::length and char_traits<wchar_t>::length.

SCFrench
  • 8,244
  • 2
  • 31
  • 61
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
3

If you were using templates wouldn't you be using std::string (which is of course templated) ?

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • 1
    Thanks for not answering my question. No, this is for writing to a basic_ofstream and allowing a person to pass in a character array buffer. – leetNightshade Mar 31 '11 at 17:47
  • 1
    @leetNightshade - that's the reason that there isn't a common templated C string std library but feel free to write your own – Martin Beckett Mar 31 '11 at 17:51
  • 1
    In that case, you're required (I think) to use the corresponding function in the traits class. – James Kanze Mar 31 '11 at 17:51
  • I already have it setup in my Engine to allow someone to write a basic_string, however if they want to pass in a buffer, perhaps created from a Network connection, then I want to have that option instead of creating a string then writing to a file. – leetNightshade Mar 31 '11 at 17:52
  • @James Kanze Wait, corresponding traits class? char_traits? What do you mean? – leetNightshade Mar 31 '11 at 17:53
  • 1
    @leetNightshade - http://stackoverflow.com/questions/5319770/what-is-the-point-of-stl-character-traits although it's not really relevent – Martin Beckett Mar 31 '11 at 17:55
  • 1
    All of the class templates in iostream take two template arguments, the charT and a traits. The traits defaults to char_traits, but using this for anything but char or wchar_t is undefined behavior. So someone has to provide one. According to the definitions in table 37. And all of the iostream classes will use it. – James Kanze Mar 31 '11 at 18:02
2
template <class T> size_t strlen( T * _arg )
{
  if ( _arg == 0 )
    return -1;
  size_t i = 0;
  while ( _arg[i] != 0 ) ++i;
  return i;
}
Naszta
  • 7,560
  • 2
  • 33
  • 49
  • 2
    Would you not want to use template specialization to call the standard version which may have been specially optimized (almost forgot what about multi-word representations are you sure the terminating character is always '\0' on wchar_t (ie could it be in the middle of a triplet)? (I am not)). – Martin York Mar 31 '11 at 17:52
  • @Martin: if you store your string in `vector` for any reason, you may need this function. Of course `find` may be good, too. – Naszta Mar 31 '11 at 17:54
0

The simple answer would be std::find, with a special end iterator that never matches anything. (In a template, you're looking for T().)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

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?

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Again, this is being used inside of a IWriteFile which allows you to write a basic_string, a CharType*, or a void* with size. So I need this for the second option if I don't want to spend the time creating a string from the buffer. For example, the buffer could've been created from a Network connection. – leetNightshade Mar 31 '11 at 17:59
0

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.

Mark B
  • 95,107
  • 10
  • 109
  • 188