Re: ANSI string from UNICODE app.
On Wed, 02 Aug 2006 01:33:37 -0700, "Mihai N." <nmihai_year_2000@yahoo.com>
wrote:
All very good advice. I would add only two points:
typically one or two places where I forget
the appropriate conversion by sizeof(TCHAR)
I had very good experience by defining
#define DIM(s) (sizeof(s)/sizeof(s[0]))
then I use it whenever I need something for "character count"
You just have to be carefull not to use it on pointers :-)
I also had a C++ construct complaining about pointers, but now
VS2005 added _countof doing exactly that.
Yes, VS2005 is using a little template hackery to exclude pointers and turn
their use into a compile-time error. It's a neat variation on the usual
method of defining a function template that returns the size:
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType
(&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))
Breaking it down, __countof_helper declares a function whose parameter is
an array reference, so only an array will do here. It returns a pointer to
a char array having the same number of elements as the argument. The macro
_countof dereferences this return value, which becomes the operand to
sizeof, yielding the number of elements in the _countof argument. What's
interesting is that __countof_helper has no body, yet _countof appears to
call the function. The trick is, sizeof doesn't evaluate its argument, so
this is all done purely at compile-time by looking at the types involved.
--
Doug Harrison
Visual C++ MVP