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
6
votes
1 answer

Arity-generic programming in Agda

How to write arity-generic functions in Agda? Is it possible to write fully dependent and universe polymorphic arity-generic functions?
effectfully
  • 12,325
  • 2
  • 17
  • 40
6
votes
1 answer

What Are C++ Run-Time Concepts?

I've been looking around on the web for details on C++ concepts lately and have found several references to what several papers call 'run-time concepts.' How exactly do they differ from compile-time concepts, why were they introduced in the first…
6
votes
3 answers

Method in base class that returns derived class type?

I have a bunch of classes that have one function in common, except that it returns a pointer to their own type. The code looks the same and I would like to move that into an abstract base class. But how can I make classes that inherit from it return…
danijar
  • 32,406
  • 45
  • 166
  • 297
6
votes
3 answers

Any suggestion for doing an arbitrary operation using given arguments of arbitrary types?

Basically i just want to do an arbitrary operation using given arguments of arbitrary types. Argument type base class is Var, and Operation is base class of the operation that will executed for given arguments. I have Evaluator class, that hold a…
uray
  • 11,254
  • 13
  • 54
  • 74
6
votes
3 answers

In the generic programming/TMP world what exactly is a model / a policy and a "concept"?

I'd like to know the precise yet succinct definitions of these three concepts in one place. The quality of the answer should depend on the following two points. Show a simple code snippet to show how and what the concept/technique is used for. Be…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
6
votes
2 answers

Constructor for nested initializer lists

Is it possible to have a generic constructor that takes any type of initializer list, even if this has nested lists within? Say you have the following partial template specialization for a class that takes in its constructor nested initializer…
aaragon
  • 2,314
  • 4
  • 26
  • 60
6
votes
1 answer

Largest Number < x?

In C++, let's say I have a number x of type T which can be an integer or floating point type. I want to find the largest number y of type T for which y < x holds. The solution needs to be templated to work transparently with both integers and…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
6
votes
1 answer

Retrieving the unclosed type of a generic type closing a generic type

I’m having an issue (probably due to my lack of familiarity of C# generics) in getting the unclosed type of a generic. I have several methods that look rather similar to the following except for the explicit validator interface in use. public…
rheone
  • 1,517
  • 1
  • 17
  • 30
6
votes
4 answers

Is possible to point that the type used for a generic method, should be an interface?

Here is my generic method code: public static IT Activate(string path) { //some code here.... } I'd want to set that generic IT must be only an interface. Is this possible?
DreadAngel
  • 772
  • 11
  • 30
5
votes
1 answer

How does `HFix` work in Haskell's multirec package?

I understand the regular fixed-point type combinator and I think I understand the higher-order fixed-n type combinators, but HFix eludes me. Could you give an example of a set of data-types and their (manually derived) fixed points that you can…
dan_waterworth
  • 6,261
  • 1
  • 30
  • 41
5
votes
2 answers

static_assert in a function declaration

I've got quite a simple function using static_assert. The trouble is that I want to static_assert on behaviour involved in the function declaration- inferring the return type, specifically. There don't seem to be any places to interject the…
Puppy
  • 144,682
  • 38
  • 256
  • 465
5
votes
4 answers

What's the point of unnamed non-type template parameters?

According to the reference, the name of a non-type template parameter is optional, even when assigning a default value (see (1) and (2)). Therefore these template structs are valid: template struct Foo {}; template struct…
andreee
  • 4,459
  • 22
  • 42
5
votes
5 answers

Library for tree search for combinatorial optimization problems

I notice that some of the "hard" combinatorial problems I come across can be cast in terms of some type of tree search like alpha-beta pruning, or beam search, or a similar algorithm. However, programming them seems like repetitively coding the same…
5
votes
4 answers

Automated Lua Binding using C++

I'm building a simple 2D game engine, and its getting bigger and bigger, exposing all of the function in Lua will be impossible: so I'm trying to automate a little bit the process, Is there anyway to get all the n arguments (with different types)…
Omarito
  • 577
  • 6
  • 22
5
votes
1 answer

Recursive transformation between nested case classes where the fields in the target are unaligned subsets of the source class

Given a pair of case classes, Source and Target, that have nested case classes, and at each level of nesting, the fields in Target are unaligned subsets of the ones in Source, is there a way to write a generic Shapeless-powered transform from Source…