Questions tagged [generic-programming]

A style of programming in which algorithms are implemented abstracting from concrete data types. Usually referred to strongly typed languages this term is usually treated as creating software which is minimal in terms of data type requirements and can be instantiated for each conforming data type without changing the callee code.

This tag should be used for questions on applying generic programming techniques. Language specific features (, ) related to issues that are not directly connected with generic programming should not be marked with this tag.

Core ideas of generic programming (GP)

  1. Lifting of an algorithm stands for finding minimal requirements for a data type interface which allow to implement the algorithm. Usually this improves reusability by widening of range of conforming data types. This process reduces coupling of the software modules and reduces dependence on secondary aspects, which typically leads to cleaner code.
  2. Specification is a process of constraining data types, typically to facilitate more efficient implementation of an algorithm.
  3. Concept stands for an interface of a data type which is an object of the lifting and specification processes.
  4. Reusable, efficient code generally depends on a balance between lifting and specification.

Widely known implementations

  1. Standard Template Library (STL) which was created by Alexander Stepanov, a GP pioneer.
  2. Generics in Java and
  3. Generics in .NET

Although less known, the first relatively widely used programming language to provide direct support for generic programming was Ada 83.

1288 questions
4
votes
2 answers

Why and how can an object file of old code use new code that uses the generic programming paradigm even though templates are static binding?

This is an entirely different question than the one I asked before which is why I'm posting this. I would like to define my topic to be a subjective question that inspires answers which explain "why" and "how". This is allowed according to the Help…
Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
4
votes
4 answers

C++11: shortest way to explicitly copy a value to use as an rvalue reference

I have a function class A { }; void f(A &&a) { ... } I want to call the function f with a variable x, but I want to run f on a copy of x, not x itself. Why can't I do something like this? f(copy(x)); But instead…
tohava
  • 5,344
  • 1
  • 25
  • 47
4
votes
1 answer

Why would a template class have an unused type?

I'm reviewing the boost units library and I am puzzled why the boost::units::unit class has an extra template parameter. Here is the example: http://www.boost.org/doc/libs/1_57_0/boost/units/unit.hpp template
4
votes
1 answer

Polymorphism in Delphi Generics

type TParent=class public member1:Integer; end; TChild=class(TParent) public member2:Integer; end; TGArray=class public function test:T; end; implementation var g:TGArray; …
Ivan Prodanov
  • 34,634
  • 78
  • 176
  • 248
4
votes
2 answers

What does T mean when used as a method signature? Is it a return type?

abstract public T iterEdges(EdgeFun func, T accum); This is for a multithreaded library for graphs. I am not asking for anything pertinent to actual implementation whatsoever, I just don't understand what the double return types mean? I am…
Arthur Collé
  • 2,541
  • 5
  • 27
  • 39
4
votes
1 answer

How to overload the product method of a typeclass

I am trying to implement a ReadJsonCodec of sorts using the automatic type class derivation mechanism in Shapeless. Here is my ReadCodecCompanionObject: object ReadCodec extends LabelledProductTypeClassCompanion[ReadCodec] { implicit object…
jedesah
  • 2,983
  • 2
  • 17
  • 29
4
votes
3 answers

Use only < comparisons in Generic Programming

I am reading the "Templates and Generic Programming" part in C++ Primer(5th Edition) but I got confused by some of the stuff there. When talking about "Writing Type-Independent Code" at P655 & P656, the author stated that "The tests in the body use…
hackjutsu
  • 8,336
  • 13
  • 47
  • 87
4
votes
1 answer

How to have generic subroutine to work in fortran with assumed size array

I have an interface block to define a generic subroutine which have an assumed size array as dummy argument (in order to be able to act on 'the middle' of a passed array, like a C pointer) and it does not compile. Here is simple exemple: module…
janou195
  • 1,175
  • 2
  • 10
  • 25
4
votes
2 answers

What kind of template specialization is used in this code sample?

I have read docs about Explicit Specialization of Class Templates and Partial Specialization of Class Templates, but don't understand what kind of specialization is used in this example (msdn links are used only due to my current environment, the…
4
votes
1 answer

how to write a generic compare function in Haxe (haxe3)

I am trying to write a generic compare function (like the c strcmp) in Haxe3 for a template type A, assuming that this template type has a less-than-or-equal-to operator "<=". I saw in the Haxe3 documentation (http://haxe.org/manual/haxe3/features)…
thor
  • 21,418
  • 31
  • 87
  • 173
4
votes
3 answers

Generic interface vs interface for each type

There are two classes X, Y which implement a Base interface There is an interface which takes care of processing the objects of each type interface Base { } class X implements Base { } class Y implements Base { } interface XProcessor { void…
4
votes
5 answers

Java generic class and wildcards

I've got a problem with generic classes in java. I've got this class: public abstract class MyMotherClass { private C item; public void setItem(C item) { this.item = item; } public C…
Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
4
votes
2 answers

How to reference an already existing type variable in a haskell type spec?

I wish to type spec a function f' defined inside a function f so that both of their type specs refer to the same type variable. However, when I try to do this, I get a compile error from the compiler, which assumes that the m outside and the m…
tohava
  • 5,344
  • 1
  • 25
  • 47
4
votes
1 answer

Overloaded template resolution

The following code prints "First". Why the 1st template is selected, while the the 2nd one seems to be more specialized and should be a better match? (I use MSVC10) I understand that it's somehow related to the fact that the second template accepts…
Igor R.
  • 14,716
  • 2
  • 49
  • 83
4
votes
4 answers

Good introduction to generics

Being compelled by the advantages I'm looking for a way to integrate generic programming into my current programming style. I would like to use generics in C# but can't find any good introductory material with some everyday examples of use. If you…
akosch
  • 4,326
  • 7
  • 59
  • 80