Le 24/03/12 12:57, Rui Maciel a écrit :
What are your thoughts on adding a standard form of proper template
metaprogramming to the C programming language?
Thanks in advance,
Rui Maciel
Metaprogramming and templates
-----------------------------
Template metaprogramming has a big drawback: the programming it is done
by the compiler. There is no way for the user to act wthin the template
environment. Even simple things like template argument checking
("concepts" in C++ terminology) is quite impossible: Bjarne has tried
for 3 years or more and wasn't able to get it right.
I have been proposing another approach for a long time, but I have been
bogged down because of my isolation and the lack of support from any
institution. Since I have to earn my life with consulting jobs, I can't
go very far.
The direction I am going is to establish real compile time functions
where you have a rich and controlled environment where you develop your
meta-programs, instead of the incredible limited environment of
templates where you do not have even an if statement!
The central points of my approach are as follows:
1: Event oriented programming. The compiler generates a sequence of
events while compiling: Start/end of function, start/end of line,
statement, etc. Any error generates an event too. You can then, in a
similar way to event oriented programming under windows or Macintosh
"add code" to be run by the compiler *within the compiler itself* that
will generate code according to the arguments of the event. You dispose
in a controlled way all the compilation environment for you. You can
query the types of the arguments, local variables, etc etc.
As a demonstration I added a profiler that uses the RDTSC machine
instruction to generate very detailed and accurate logs of time passed
at each statement/function.
2: Calling template code from within your code. You can use special
syntax to call your own template procedures by writing in your code
ListGen<Var=mylist,Type=mylistType,InitialSize=10>
That is an "undefined variable" error. You subclass that error, and
you are all set, able to generate code for a list as you wish.
The programming language used to program in the metaprogramming
environment is.... well, you guessed! IT IS THE C language. You
program all your meta-programming code in C, you compile it with
lcc-win, and the compiler loads it when it starts.
The input of your function is the program text at the point of the
event, i.e. you will find the text of the template in the code buffer.
The output of your function is text: the generated code for the
functionality you want. That code is re-injected into the compiler input
stream and processed as you wuld have typed it, just as the
templates of C++.
Of curse this is MUCH more advanced. You do not need to bother about
"concepts": you can check your arguments yourself within your
metaprogram: you have all the normal constructs of C99 at your disposal.
You aren't dependent of the compiler "understanding correctly" what you
want since you can EXACTLY generate what you want.
Look, this is not just empty talk. I have added for instance the require
and ensure statements of the Eiffel language to an experimental version
of the compiler to implement the "Design by contract" paradigm.
I started implementing objective C, but it is not ready yet. I have
several customers to serve, and two kids+wife...
The reaction in this group has been abysmal, as always. Anyway, here are
my ideas again
jacob