Discussion Locked

Call the correct string-related function

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
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?

2 replies

by Mark W. Kaelin

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.

by john torjo

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

This discussion is closed to new replies.