Page 1 of 1
Making c++ templates runtime-linkable
Posted: Sun Dec 05, 2004 9:52 am
by Candy
Is this possible? In the worst case with a special compiler for runtime starting the program...
I have thought about it myself and I think that one could develop a meta-language for templatized classes in terms of the size of the object concerned and calls to normal or nonnormal functions in the child object, where the template load-time compiler (awkward term...) changes these to the actual values, compiles it and then links in the resulting code. I have thought about it, but I didn't find any problems with it, aside from an average-complexity compiler in addition to the normal suite, and a linker mod so it doesn't trip.
Since this hasn't been done before (as far as I can tell) there must be something I'm missing. I can't see it myself though.
Thanks in advance for help,
Candy
Re:Making c++ templates runtime-linkable
Posted: Mon Dec 06, 2004 1:47 am
by Solar
Hmmm... I'm always amazed at what "crazy" ideas come up here.
Templates, by their very design, are
compile-time entities. The availability of member functions called in the template, and for which datatypes you need instances of the template, is both checked at compile-time.
You would have to write up a pre-compiler that takes any templates from the C++ code and changes them into something
very different...
My head spins. Why for the sake of your favourite deity would you want to express run-time functionality with templates? There are other design patterns / technologies to achieve that...?
Re:Making c++ templates runtime-linkable
Posted: Mon Dec 06, 2004 2:06 am
by Candy
Solar wrote:
Hmmm... I'm always amazed at what "crazy" ideas come up here.
Templates, by their very design, are
compile-time entities. The availability of member functions called in the template, and for which datatypes you need instances of the template, is both checked at compile-time.
You would have to write up a pre-compiler that takes any templates from the C++ code and changes them into something
very different...
My head spins. Why for the sake of your favourite deity would you want to express run-time functionality with templates? There are other design patterns / technologies to achieve that...?
My favourite deity would be the computer btw
The idea would be that you could make new implementations of STL (or other templatized) classes such as sort etc. and post-apply them to old programs. Stretching the idea, you could make a "settings" window in which you could choose the methods to choose, so you could hand-tweak each application to work as fast as possible or with the least amount of memory. That was the main idea.
Of course, it'd make my modular user level code easier too.
Re:Making c++ templates runtime-linkable
Posted: Tue Dec 07, 2004 4:17 am
by Solar
Doesn't work, and hardcore C++ coders will probably rip your heart out for trying.
Honestly, I can't picture a way to do it, short of reverse-engineering binary code to C++ source (HA!) and recompiling...
Template code is compile-time. That's inherent in its design, and coders rely on it in ways I found hard to imagine before I was shown what black magic can be done with 'em. What's making the C++ STL sort() faster than even the best C qsort() is that the C++ template does not have to go through a function pointer for every compare()... because the template sort() is resolved by the compiler already.
And even if possible at STL level, professional software vendors won't like it a bit when a flick of a config flag can retroactively introduce code into their apps that they don't have control over.
What you
can do is to provide an
OS specific C++ library that can be updated retroactively, and leave the choice (STL? CandyLib?) to the application programmer.