Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Template metaprogramming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Static polymorphism== [[Type polymorphism|Polymorphism]] is a common standard programming facility where derived objects can be used as instances of their base object but where the derived objects' methods will be invoked, as in this code <syntaxhighlight lang="cpp"> class Base { public: virtual void method() { std::cout << "Base"; } virtual ~Base() {} }; class Derived : public Base { public: virtual void method() { std::cout << "Derived"; } }; int main() { Base *pBase = new Derived; pBase->method(); //outputs "Derived" delete pBase; return 0; } </syntaxhighlight> where all invocations of <code>virtual</code> methods will be those of the most-derived class. This ''dynamically polymorphic'' behaviour is (typically) obtained by the creation of [[vtable|virtual look-up table]]s for classes with virtual methods, tables that are traversed at run time to identify the method to be invoked. Thus, ''run-time polymorphism'' necessarily entails execution overhead (though on modern architectures the overhead is small). However, in many cases the polymorphic behaviour needed is invariant and can be determined at compile time. Then the [[Curiously Recurring Template Pattern]] (CRTP) can be used to achieve '''static polymorphism''', which is an imitation of polymorphism in programming code but which is resolved at compile time and thus does away with run-time virtual-table lookups. For example: <syntaxhighlight lang="cpp"> template <class Derived> struct base { void interface() { // ... static_cast<Derived*>(this)->implementation(); // ... } }; struct derived : base<derived> { void implementation() { // ... } }; </syntaxhighlight> Here the base class template will take advantage of the fact that member function bodies are not instantiated until after their declarations, and it will use members of the derived class within its own member functions, via the use of a <code>static_cast</code>, thus at compilation generating an object composition with polymorphic characteristics. As an example of real-world usage, the CRTP is used in the [[Boost library|Boost]] [[iterator]] library.<ref>{{Cite web|url=http://www.boost.org/libs/iterator/doc/iterator_facade.html|title = Iterator Facade - 1.79.0}}</ref> Another similar use is the "[[Barton–Nackman trick]]", sometimes referred to as "restricted template expansion", where common functionality can be placed in a base class that is used not as a contract but as a necessary component to enforce conformant behaviour while minimising code redundancy.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)