Search code examples
c++oopgeneric-programmingdynamic-bindingstatic-binding

Why and how can an object file of old code use new code that uses the generic programming paradigm even though templates are static binding?


This is an entirely different question than the one I asked before which is why I'm posting this.

I would like to define my topic to be a subjective question that inspires answers which explain "why" and "how". This is allowed according to the Help Center rules.

In order to make my question more constructive, I provide you with resources that better explain my topic.

From the C++ SuperFAQ under the question

"Can you give me a simple reason why virtual functions (dynamic binding, dynamic polymorphism) and templates (static polymorphism) make a big difference?"

The author says, "...a programmer might write some code that is called by a framework that was written by their great, great grandfather. There’s no need to change great-great-grandpa’s code. In fact, for dynamic binding with virtual functions, it doesn’t even need to be recompiled.Even if all you have left is the object file and the source code that great-great-grandpa wrote was lost 25 years ago, that ancient object file will call the new extension without anything falling apart."

He goes on to say, "That is extensibility, and that is OO and generic programming for powerful reusable abstraction."

Furthermore, I recently read a paper written by the creator of C++ Bjarne Stroustrup called, "Why C++ is not just an Object-Oriented Programming Language". In his paper, he defines a language or technique as object-oriented if and only if it directly supports:

  1. Abstraction – providing some form of classes and objects.

  2. Inheritance – providing the ability to build new abstractions out of existing ones.

  3. Run-time polymorphism – providing some form of run-time binding.

He also briefly mentions generic programming.

6.7 Generic Programming - A major theme in the C ++ community over the last few years has been the development of techniques exploiting the template mechanism.

From reading these two resources I can understand how old code is able to use new code by means of the object-oriented paradigm. However, I do not understand how old code can use new code by use of generic programming (in this case templates) because generic programming uses static binding. From my previous related question Ben Voigt commented that,

"For "old" template code to be combined with "new" template code, it is necessary for both to be compiled together."

The C++ SuperFAQ seems to imply (for both OO and generic programming) that the old code should not have to be recompiled with the new code in order to use the new code, and that you should just need the object file from the old code. Thereby, maintaining code reusability.

Could someone please answer "why" and "how" an object file of old code can use new code that uses the generic programming paradigm even though templates are static binding?

Edit

I would like to explain the answer below in a little more detail since it will help me understand more and may help others understand. Since generic programming uses static binding, both the source code of the old code and that of the new code must be re-compiled "together" for code reusability to take effect. This means, if you only have the object file of the old code, you would not be able to use the new code with it since that would require dynamic/late binding which is binding at run-time.


Solution

  • The short answer is that it can't. I'd guess that's what the first sentence of the answer you linked was intended to say:

    They can improve reuse by letting old code call new code provided at run time (virtual functions) or compile time (templates).

    [Emphasis added]

    For existing code to make use of templates, you do have to re-compile. That means you have to start from (some form of) source code, not object files (at least not what most people think of as normal object files, anyway).