General discussion

Locked

Call the correct string-related function

By Mark W. Kaelin Editor ·
Do you find it difficult to write generic string-related functions when calling primitive string functions? Will this explanation of how to call the correct string-related function make it easier?

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Another option

by Mark W. Kaelin Editor In reply to Call the correct string-r ...

I received this e-mail today. I thought it might be of interest:

For standard library template strings I do:

#ifdef _UNICODE
#define _tstring wstring
#else
#define _tstring string
#endif

Works perfectly and simply extends Microsoft's _Tand TCHAR approach.

Collapse -

Not quite ok for generic programming

by John Torjo In reply to Another option

Hi,

I quite agree with the above technique for day-to-day programming. However, there are a few drawbacks for generic programming:
- it's Microsoft specific
- it does not scale for generic programming. When doing generic programming, you cannotassume the character type passed to a generic function.
Consider the following:

// should work for 'const char *' strings,
// 'const wchar_t *' strings, etc.
template< class char_type>
int str_length( const char_type * str)
{ /* code */ }

A client of your code might use:
int nLen = str_length( "this is a test"); /* 'const char *' */

OR

int nLen = str_length( L"this is a test"); /* 'const wchar_t *' */

or, even both.


Best,
John

Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums