How C++ Templates Are Used for Generic Programming

Generic programming is a key paradigm for developing reusable software components. The inherent support for generic constructs is therefore important in programming languages. As for C++, the generic construct, templates, has been supported since the language was first released. However, little is currently known about how C++ templates are actually used in developing real software. In this study, we conduct an experiment to investigate the use of templates in practice. We analyze 1,267 historical revisions of 50 open source systems, consisting of 566 million lines of C++ code, to collect the data of the practical use of templates. We perform statistical analyses on the collected data and produce many interesting results. We uncover the following important findings: (1) templates are practically used to prevent code duplication, but this benefit is largely confined to a few highly used templates; (2) function templates do not effectively replace C-style generics, and developers with a C background do not show significant preference between the two language constructs; (3) developers seldom convert dynamic polymorphism to static polymorphism by using CRTP (Curiously Recursive Template Pattern); (4) the use of templates follows a power-law distribution in most cases, and C++ developers who prefer using templates are those without other language background; (5) C developer background seems to override C++ project guidelines. These findings are helpful not only for researchers to understand the tendency of template use but also for tool builders to implement better tools to support generic programming.

[1]  Michael Hoppe,et al.  Do developers benefit from generic types?: an empirical comparison of generic and raw types in java , 2013, OOPSLA.

[2]  F. Tripet,et al.  DENSITY‐DEPENDENT PROCESSES IN THE POPULATION DYNAMICS OF A BIRD ECTOPARASITE CERATOPHYLLUS GALLINAE , 1999 .

[3]  Gabriel Dos Reis Templates Aliases for C++ , 2003 .

[4]  J. Reist,et al.  An empirical evaluation of several univariate methods that adjust for size variation in morphometric data , 1985 .

[5]  F. Stephen Dobson,et al.  Increasing returns in the life history of Columbian ground squirrels , 1999 .

[6]  Stefan Hanenberg,et al.  An empirical study of the influence of static type systems on the usability of undocumented software , 2012, OOPSLA '12.

[7]  Bjarne Stroustrup,et al.  The C++ Programming Language, 4th Edition , 2013 .

[8]  Bjarne Stroustrup,et al.  Rejuvenating C++ programs through demacrofication , 2012, 2012 28th IEEE International Conference on Software Maintenance (ICSM).

[9]  Richard Shine,et al.  Life history consequences of nest-site variation in tropical pythons (Liasis fuscus) , 1999 .

[10]  Herb Sutter,et al.  C++ Coding Standards: 101 Rules, Guidelines, and Best Practices (C++ in Depth Series) , 2004 .

[11]  Leo A. Meyerovich,et al.  Empirical analysis of programming language adoption , 2013, OOPSLA.

[12]  Scott Meyers,et al.  Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library , 2001 .

[13]  Peter H. Becker,et al.  Effects of parental quality and effort on the reproduction of common terns , 1999 .

[14]  Richard Shine,et al.  The adjustment of reproductive threshold to prey abundance in a capital breeder , 1999 .

[15]  Baowen Xu,et al.  A metrics-based comparative study on object-oriented programming languages , 2015, ICSE 2015.

[16]  Audris Mockus,et al.  An empirical study of goto in C code from GitHub repositories , 2015, ESEC/SIGSOFT FSE.

[17]  Douglas Gregor,et al.  C++ Templates: The Complete Guide , 2002 .

[18]  J. Pearl,et al.  Confounding and Collapsibility in Causal Inference , 1999 .

[19]  Gustavo Pinto,et al.  A large-scale study on the usage of Java's concurrent programming constructs , 2015, J. Syst. Softw..

[20]  Danny Dig,et al.  How do developers use parallel libraries? , 2012, SIGSOFT FSE.

[21]  Lionel C. Briand,et al.  A systematic and comprehensive investigation of methods to build and evaluate fault prediction models , 2010, J. Syst. Softw..

[22]  Bjarne Stroustrup,et al.  The Design and Evolution of C , 1994 .

[23]  Sven Apel,et al.  Does the discipline of preprocessor annotations matter?: a controlled experiment , 2014, GPCE '13.

[24]  Michael D. Ernst,et al.  An Empirical Analysis of C Preprocessor Use , 2002, IEEE Trans. Software Eng..

[25]  James J. Elser,et al.  THE STOICHIOMETRY OF CONSUMER‐DRIVEN NUTRIENT RECYCLING: THEORY, OBSERVATIONS, AND CONSEQUENCES , 1999 .

[26]  W. M. Lonsdale,et al.  GLOBAL PATTERNS OF PLANT INVASIONS AND THE CONCEPT OF INVASIBILITY , 1999 .

[27]  Madhusudan Katti,et al.  Annual variation in fat storage by a migrant warbler overwintering in the Indian tropics , 1999 .

[28]  Ali Mili,et al.  An empirical study of programming language trends , 2005, IEEE Software.

[29]  Maurice H. Halstead,et al.  Elements of software science , 1977 .

[30]  Akif Günes Koru,et al.  Comparing high-change modules and modules with the highest measurement values in two large-scale open-source products , 2005, IEEE Transactions on Software Engineering.

[31]  L. Bruinzeel,et al.  The benefit of large broods in barnacle geese : a study using natural and experimental manipulations , 1999 .

[32]  Antonio Lima,et al.  Coding Together at Scale: GitHub as a Collaborative Social Network , 2014, ICWSM.

[33]  Jaakko Järvi,et al.  Variadic templates for C++ , 2007, SAC '07.

[34]  Sunghun Kim,et al.  Bug Classification Using Program Slicing Metrics , 2006, 2006 Sixth IEEE International Workshop on Source Code Analysis and Manipulation.

[35]  David Leon,et al.  Finding failures by cluster analysis of execution profiles , 2001, Proceedings of the 23rd International Conference on Software Engineering. ICSE 2001.

[36]  Y. Benjamini,et al.  Controlling the false discovery rate: a practical and powerful approach to multiple testing , 1995 .

[37]  Harald C. Gall,et al.  A study of language usage evolution in open source software , 2011, MSR '11.

[38]  Yuming Zhou,et al.  Examining the Potentially Confounding Effect of Class Size on the Associations between Object-Oriented Metrics and Change-Proneness , 2009, IEEE Transactions on Software Engineering.

[39]  A. Dixon,et al.  Population dynamics of a tree-dwelling aphid: regulation and density-independent processes , 1999 .

[40]  Yuming Zhou,et al.  On the ability of complexity metrics to predict fault-prone classes in object-oriented systems , 2010, J. Syst. Softw..

[41]  Yuming Zhou,et al.  How do developers use C++ libraries? An empirical study , 2015, SEKE.

[42]  Romain Robbes,et al.  How developers use the dynamic features of programming languages: the case of smalltalk , 2011, MSR '11.

[43]  Mark E. J. Newman,et al.  Power-Law Distributions in Empirical Data , 2007, SIAM Rev..

[44]  S G Ganesh 60 Tips on Object Oriented Programming , 2008 .

[45]  Yuming Zhou,et al.  An Empirical Study on C++ Concurrency Constructs , 2015, 2015 ACM/IEEE International Symposium on Empirical Software Engineering and Measurement (ESEM).

[46]  Emerson R. Murphy-Hill,et al.  Java generics adoption: how new features are introduced, championed, or ignored , 2011, MSR '11.

[47]  Yuming Zhou,et al.  An empirical study on the adoption of C++ templates: Library templates versus user defined templates , 2014, SEKE.

[48]  Nicolai M. Josuttis The C++ Standard Library: A Tutorial and Reference , 2012 .

[49]  Dan Grossman,et al.  How programming languages will co-evolve with software engineering: a bright decade ahead , 2014, FOSE.

[50]  Jeff Leips,et al.  The comparative expression of life-history traits and its relationship to the numerical dynamics of four populations of the least killifish , 1999 .

[51]  Hareton K. N. Leung,et al.  An in-depth study of the potentially confounding effect of class size in fault prediction , 2014, TSEM.

[52]  Stan Jarzabek,et al.  An Empirical Study on Limits of Clone Unification Using Generics , 2005, SEKE.

[53]  Matthew H. Austern Generic programming and the STL - using and extending the C++ standard template library , 1999, Addison-Wesley professional computing series.

[54]  S. Tucker Taft,et al.  Information technology — Programming Languages — Ada , 2001 .

[55]  Carlo A. Furia,et al.  A Comparative Study of Programming Languages in Rosetta Code , 2014, 2015 IEEE/ACM 37th IEEE International Conference on Software Engineering.

[56]  Hridesh Rajan,et al.  Mining billions of AST nodes to study actual and potential usage of Java language features , 2014, ICSE.

[57]  Premkumar T. Devanbu,et al.  A large scale study of programming languages and code quality in github , 2014, SIGSOFT FSE.

[58]  HanenbergStefan An experiment about static and dynamic type systems , 2010 .

[59]  G. Uetz,et al.  Estimating fitness : a comparison of body condition indices , 1996 .

[60]  Pamela O’Neil,et al.  SELECTION ON FLOWERING TIME: AN ADAPTIVE FITNESS SURFACE FOR NONEXISTENT CHARACTER COMBINATIONS , 1999 .

[61]  Sebastian Kleinschmager,et al.  Do static type systems improve the maintainability of software systems? An empirical study , 2012, 2012 20th IEEE International Conference on Program Comprehension (ICPC).

[62]  Bjarne Stroustrup,et al.  C++ Programming Language , 1986, IEEE Softw..

[63]  Emerson R. Murphy-Hill,et al.  The Reaction of Open-Source Projects to New Language Features: An Empirical Study of C# Generics , 2013, J. Object Technol..

[64]  Scott Meyers,et al.  Effective C++: 55 Specific Ways to Improve Your Programs and Designs (3rd Edition) , 1991 .

[65]  Jaakko Järvi,et al.  An extended comparative study of language support for generic programming , 2007, Journal of Functional Programming.

[66]  J. Koella,et al.  Constraints on the reproductive value of vertical transmission for a microsporidian parasite and its female‐killing behaviour , 1999 .

[67]  Stefan Hanenberg,et al.  An experiment about static and dynamic type systems: doubts about the positive impact of static type systems on development time , 2010, OOPSLA.

[68]  Romain Robbes,et al.  Object-oriented software extensions in practice , 2013, Empirical Software Engineering.

[69]  Emerson R. Murphy-Hill,et al.  Adoption and use of Java generics , 2012, Empirical Software Engineering.