Making c++ templates runtime-linkable

Programming, for all ages and all languages.
Post Reply
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Making c++ templates runtime-linkable

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Making c++ templates runtime-linkable

Post by Solar »

Hmmm... I'm always amazed at what "crazy" ideas come up here. :o :D

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...?
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Making c++ templates runtime-linkable

Post by Candy »

Solar wrote: Hmmm... I'm always amazed at what "crazy" ideas come up here. :o :D

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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Making c++ templates runtime-linkable

Post 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.
Every good solution is obvious once you've found it.
Post Reply