I don't believe it. I've never seen an editor that wouldn't
allow you to write just about anything. Do you mean it colors
it wrong, or that it indents incorrectly afterwards? (In the
latter case, of course, I'd wonder about the editor. All of the
various editors I've used for C++ know about the preprocessor,
and don't take macros into account for indenting.)
Or did you mean editor in the human sense: someone who reads
your code and corrects possible errors? Because human readers
do care about keywords---the presence of class in the macro will
immediately suggest a keyword to them, and probably cause some
confusion. Of course, for human readers, clazz isn't any
better; what you probably want is classname, or something like
that. (In the places I've worked, of course, classe or Klasse
would also work very well. A small advantage of being in a
non-English speaking environment
.)
Of course, any human editor would reject the code anyway, as
unnecessary and improper use of a macro, when better solutions
exist without the macro. The standard solution here is to use
some sort of map, mapping the name to a factory method (or a
factory interface). The map can be initialized in one of two
ways:
-- Dynamically, by initializers of static data. The author of
each class arranges for a static instance of something which
inserts his factory method or factory class into the
map---I'll usually map to a pointer to a factory interface,
with a constructor which does the registration; each class
derives from the interface, providing the necessary
function, and defines a static instance of its derived
class.
Of course, in this case, the map itself should be a
singleton, to avoid order of initialization issues with it.
I've even used this method with dynamic linking: when the
lookup in the map failed, I derived the name of a dynamic
object (.dll or .so, depending on the system) and tried to
load it, then tried again in the map. If loading it loaded
a static instance of the factory object, the second lookup
would find the factory object. In this case, you can even
find classes that weren't written yet when the program was
installed.
Of couse, you don't want to do this unless it's necessary.
-- Statically, using a simple array of struct's:
struct FactoryMapEntry
{
char const* className ;
Base* (* factoryMethod)() ;
} ;
This has the advantage of simplicity, and avoids any issues
of order of initialization, but it does mean that you have
to write the initialization of the map in C++, compile it,
and link it into your code. Or rather, someone or something
has to write it; I imagine that in most cases, you'd
generate it automatically by means of a shell script
invoked from the makefile. Supposing, of course, some well
defined naming convention for the factory methods (e.g.
createXXX for class XXX---or maybe create< XXX >(), with an
appropriately defined function template).
For lookup, of course, std::find_if with an appropriate
predicate will do the trick nicely.
This method means that once (statically) linked, the
available classes cannot change, and the complete map is
available everywhere during initialization of static
objects.
I can't remember using this for factory methods, but I do it
often enough for other things that I've a generic template
to automatically generate the table entries and the
necessary predicate for std::find_if.