Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Templates and Genericity 121 ed inside f to render compilable code. This is called an "implicit interface," in con- trast to a base class's explicit interface. It achieves the same goal of polymorphism-- writing code that operates on multiple types--but in a very different way. Static polymorphism is best at: Uniform manipulation based on syntactic and semantic interface: Types that obey a syntactic and semantic interface can be treated uniformly. Interfaces are syntac- tic and implicit (not signature-based and explicit), and so allow any type substi- tution that fits a given syntax. For example, given the statement int i = p->f( 5 ): If p is a pointer to a Base class type, this calls a specific interface function, such as perhaps a virtual int f(int). But if p is of a generic type, this call can bind to a myriad of things, including that it might invoke an overloaded operator-> that returns a type defining the function X f(double) where X is convertible to int. Static type checking: All types are checked statically. Static binding (prevents separate compilation): All types are bound statically. Efficiency: Compile-time evaluation and static binding allow optimizations and efficiencies not available with dynamic binding. Decide on your priorities, and use each type of polymorphism for its strengths. Prefer to blend both kinds of polymorphism to combine their benefits while trying