Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++, the term/idiom for programming using template

Tags:

c++

templates

I keep reading the term :

  1. template programming
  2. generic programming
  3. meta-programming
  4. maybe another idiom/term..

for any c++ code that use template, which one is the correct or more accurate term of this?

like image 241
uray Avatar asked Dec 19 '10 15:12

uray


2 Answers

AFAIK:

  1. Template programming is just referring to the classic "programming with templates", i.e. "I have a function/class that I want to make usable with any type, I'll just make it template".

    It can also be can also be seen as the "catch-all" category that includes any programming technique that employs templates.

  2. Generic programming can be synthetically described as the programming paradigm used by the STL.

    Wikipedia defines it as

    a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters

    IMHO, it's better to say that all the containers are designed to be used with any type (without sacrificing type safety) and algorithms are designed to be generic enough to work on any container type (as long as it's sensible to use them, obviously, i.e. it makes no sense to sort an unordered container).

    Notice that generic programming (with this definition) does not strictly require the use of templates, in facts it can be achieved with inheritance and dynamic polymorphism (thanks to Ben Voigt).

    In general, I'd say that template programming and generic programming partially overlap, and many people use the terms generic programming and template programming interchangeably.

  3. Template metaprogramming is a programming style in which templates are used to perform compile-time computations/decisions/checks normally not doable without templates (statical assertions, compile-time constants computations, ...).

    Such code is often quite contrived, since C++ wasn't designed for this style of programming (which was actually "discovered" later), and may look unfamiliar to C++ programmers also because it often gets near to functional programming (without having nice syntax facilities for it) instead of following the imperative paradigm normally used in C++.

like image 60
Matteo Italia Avatar answered Oct 05 '22 23:10

Matteo Italia


It's usually referred to as generic programming.

Template meta programming is something else than normal use of templates, in TMP types are manipulated at compile time (see boost.Mpl).

like image 31
Motti Avatar answered Oct 06 '22 00:10

Motti