Compile-Time Polymorphism on a Diet

Generic classes can be used to improve performance by allowing compile-time polymorphism. But their applicability is limited, and they might bloat the object code. We advocate a programming principle whereby an inner class (of a generic class) should not actually nest unless it depends on every generic parameter; instead, we propose to use an alias to a non-nested class that minimizes the dependencies. Conforming to this principle gives rise to previously unconceived programming semantics that expand the applicability of compile-time polymorphism and reduces its bloat. Our contribution is thus a programming technique that generates faster and smaller programs. We apply our ideas to GCC’s STL containers and iterators, and we demonstrate notable speedups and reduction in object code size (real application runs 1.2x to 2.1x faster and STL code is 1x to 25x smaller). We conclude that standard APIs (like STL) should be amended to reflect the proposed principle in the interest of efficiency and compactness. Such modifications will not break old code, simply increase flexibility.

By: Dan Tsafrir; Robert W. Wisniewski; David F. Bacon; Bjarne Stroustrup

Published in: RC24779 in 2009


This Research Report is available. This report has been submitted for publication outside of IBM and will probably be copyrighted if accepted for publication. It has been issued as a Research Report for early dissemination of its contents. In view of the transfer of copyright to the outside publisher, its distribution outside of IBM prior to publication should be limited to peer communications and specific requests. After outside publication, requests should be filled only by reprints or legally obtained copies of the article (e.g., payment of royalties). I have read and understand this notice and am a member of the scientific community outside or inside of IBM seeking a single copy only.


Questions about this service can be mailed to .