On Sat, 11 Oct 2014 14:55:45 -0500, Paavo Helde
<
myfir...@osa.pri.ee> wrote:
I just ran across this message. Sorry for the late reply.
>DSF <nota...@address.here> wrote in
>
news:nnsi3a94gsmhji594...@4ax.com:
>
>> Hello group!
>>
>> So far, my string template class has been working well. It's based
>> on this code to select 8-bit or 16-bit strings:
>>
>> template <class CH> class FString
>> {
>> ...
>> }
>>
>> typedef FString<char> FStringA;
>> typedef FString<wchar_t> FStringW;
>
>Note that on a lot of platforms wchar_t is 32-bit.
Yes, I know. The FString class makes a lot of calls to functions
that assume wide == 16-bit. But I doubt any of my code will ever see
the light of day beyond my own projects, so portability is not my
chief concern right now.
>
>> #ifdef UNICODE
>> #define FString FStringW
>> #else
>> #define FString FStringA
>> #endif
>
>This does not respect namespaces and does not follow the rule that macro
>names are all-capitals. On top of that, having the macro name the same as
>the class name is just confusing as hell. What's wrong with a typedef and
>using different names?
I know macros bridge across all namespaces. And, more importantly
to me at this time, bridge across all classes. (Come to think of it,
inside a class is a namespace.)
In this case, I believe the very fact that the macro bridges all
boundaries is an asset. After all, the UNICODE definition is most
often used as a program-wide indication of 8-bit or 16-bit characters
in Window$.
Out of curiosity (and future use), how could I write a
namespace-aware version of the above?
To the best of my knowledge, it is not a rule that macros must be
all caps. On my compiler, the function getchar which is listed as a
Standard function, is a macro. None of the functions implemented as
macros are all upper case. I personally use all upper case for macros
that define an immediate value and use mixed case for macros that
simplify a function call.
As to "confusing as hell," actually I thought having the names the
same would be confusing, if it even worked. But it's not. They are
tucked away at the end of FString.h and forgotten about. If I am
writing code dependant on the UNICODE definition, I use FString. If I
need a specific type, such as ANSI (or even UTF-8), I explicitly use
FStringA.
>As for your questions, the macro substitution is done (at least
>conceptually) in the preprocessor stage, before compilation. I.e. all
>occurences of FString after the #define are replaced textually with
>FStringW or FStringA, before the C++ compiler ever gets to see the code.
>I.e. if you have 'FStringA ansistr;' then the C++ compiler will replace
>it internally by FString<char>, but this happens in a later compilation
>stage where the preprocessing has already been done, so FString is not a
>macro any more.
>
>PS. I'm just curious in what way your string class is better than
>std::basic_string?
It's not. But writing it has taught me a lot about templates. I've
also written my own array-type templates for the same reason. Most
books and online articles cover only the most basic elements of
templates, leaving me with the thought "Fine! I Understand. But how do
I do this?" Once I started writing my own, I was able to formulate
better questions and thus, search for better information.
By the way, I've found - at least for me - that studying the STL to
learn to write templates is, to borrow your phrase "confusing as
hell". :o)
>Cheers
>Paavo
>
Thanks for the input,