user923005 said:
I thought about this for a while. At first, I thought that templates
in C was a very good idea. They lead to simpler, easier to understand
and more generic versions of data structures and algorithms. And to
create templates, you would not need all the machinery of C++. But
then I started to see holes in it. For instance, because you do not
have operator overloading, you cannot extend the pretty version of
sorting algorithms which have a compare operator, since C strings
would not compare correctly. Also, things like a complex number
template that I suddendly want to reuse for extended precision are not
going to work properly either. So the templates are really going to
be crippled in C. When I thought about it long enough I finally
decided that I would never use them, because I would use C++ to write
the algorithms like that anyway. In short, I think that the idea is
great on the surface but when you think about it they would be clearly
inferior to C++ templates because we do not have operator overloading
and constructors and other things that make them work so nicely in C+
+.
But then, why not compile time functions?
The basic idea of templates is to allow a function that
generates code at compile time.
In C++ the code generation is done by the compiler, and we have
1) The template definition time, where the compiler parses a
definition of a template and stores it away.
2) The template expansion time, when the template is used.
Here the compiler "expands" the template code, doing the
replacement of the given arguments within the template
text.
In lcc-win, you can define a compile time function, that
is, a function that will be called during the compilation,
and that can receive formal arguments. This function will
produce C code as output, just like a template, but with
fundamental differences:
A) You have the full C language for your code generation.
Within C++, you *can* trick the compiler to use IF ELSE
and other constructs, but it is very difficult, and purely
functional. You CAN'T save the state of the generation
or call subroutines, etc etc. You are very limited, and
code generation within templates suffers from this.
Compile time functions are EXECUTABLE code (i.e. already
compiled code) that have the full power of C to generate
code.
B) There are two ways to call a compile time function:
B.1: Direct: list<int>
B.2 Indirect: When an event fires. The compiler produces
a stream of events: function start, variable definition start,
statement end, statement start, block end, block start,
comment start, etc. You can the tell the compiler to call
your function when a specific event fires, in the same
way as aspect oriented programming does.
All this is working in prototype form. I wrote a full variant
of C with Betrand Meyer's programming by contract (require/ensure)
with it, and used the features that I needed to do that.
But I am completely alone doing this, and I got discouraged by the
negative attitude in this group. It is a pity because this is much
more powerful and interesting than C++.
The problem with C++ is that the compiler is too complex. This feature
allows to keep the compiler very simple and have MUCH MORE power at
your fingertips!