Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic programming vs. Metaprogramming

Tags:

What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across:

In addition to C’s style of procedural programming, C++ directly supports certain forms of object-oriented programming, generic programming, and metaprogramming.

in reference to C++. So apparently they're different?

like image 695
afkbowflexin Avatar asked Oct 14 '10 21:10

afkbowflexin


People also ask

Are generics metaprogramming?

Generics are used for metaprogramming in Ada. They are useful for abstract algorithms that share common properties with each other.

What is the point of metaprogramming?

Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze or transform other programs, and even modify itself while running.

What is a metaprogramming language?

Simply put, Metaprogramming involves writing code that can. Generate code. Manipulate language constructs at the run time. This phenomenon is known as Reflective Metaprogramming or Reflection .

What is meant by generic programming?

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


2 Answers

Metaprogramming, in a broad sense, means writing programs that yield other programs. E.g. like templates in C++ produce actual code only when instantiated. One can interpret a template as a program that takes a type as an input and produces an actual function/class as an output. Preprocessor is another kind of metaprogramming. Another made-up example of metaprogramming:a program that reads an XML and produces some SQL scripts according to the XML. Again, in general, a metaprogram is a program that yields another program, whereas generic programming is about parametrized(usually with other types) types(including functions) .

EDITED after considering the comments to this answer

like image 32
Armen Tsirunyan Avatar answered Oct 08 '22 05:10

Armen Tsirunyan


  • Programming: Writing a program that creates, transforms, filters, aggregates and otherwise manipulates data.
  • Metaprogramming: Writing a program that creates, transforms, filters, aggregates and otherwise manipulates programs.
  • Generic Programming: Writing a program that creates, transforms, filters, aggregates and otherwise manipulates data, but makes only the minimum assumptions about the structure of the data, thus maximizing reuse across a wide range of datatypes.

As was already mentioned in several other answers, the distinction can be confusing in C++, since both Generic Programming and (static/compile time) Metaprogramming are done with Templates. To confuse you even further, Generic Programming in C++ actually uses Metaprogramming to be efficient, i.e. Template Specialization generates specialized (fast) programs from generic ones.

Also note that, as every Lisp programmer knows, code and data are the same thing, so there really is no such thing as "metaprogramming", it's all just programming. Again, this is a bit hard to see in C++, since you actually use two completely different programming languages for programming (C++, an imperative, procedural, object-oriented language in the C family) and metaprogramming (Templates, a purely functional "accidental" language somewhere in between pure lambda calculus and Haskell, with butt-ugly syntax, since it was never actually intended to be a programming language.)

Many other languages use the same language for both programming and metaprogramming (e.g. Lisp, Template Haskell, Converge, Smalltalk, Newspeak, Ruby, Ioke, Seph).

like image 152
Jörg W Mittag Avatar answered Oct 08 '22 05:10

Jörg W Mittag