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
8
votes
5 answers

what could this generic class declaration could mean?

I know this isn't a good question to ask and I might get cursed to ask it but I cannot find any place to get help on this question Below is a Generic class that appeared in my interview question (which I have already failed). The question was to…
anonymous
  • 2,009
  • 3
  • 16
  • 13
8
votes
2 answers

Operating on Scala collections in generic way

I wrote the function to find longest common subsequence (LCS). For example, for two sequences of chars BANANA and ATANA it returns AANA. Implementation is naive inefficient adaptation of recursive algorithm, but it is irrelevant for purpose of this…
8
votes
0 answers

square bracket and the pair type in C++

I read a piece of code from a book but I do not really understand the grammar, const auto& [local_min, local_max] = minmax(A[i], A[i+1]); where A is vector of int and local_min and local_max are int. I know minmax returns a pair, but what does the…
Sean
  • 2,649
  • 3
  • 21
  • 27
8
votes
2 answers

Detect compile-time literals and constants

Say I wanted to write a generic class that maintains an integer that always stays between two values. Something like this: template class MyInt { private: int m_int; public: // Constructors,…
Drag-On
  • 362
  • 2
  • 9
8
votes
2 answers

Get type of a "singleton type"

We can create a literal types via shapeless: import shapeless.syntax.singleton._ var x = 42.narrow // x: Int(42) = 42 But how can I operate with Int(42) as a type if it's even impossible to create type alias type Answ = Int(42) // won't compile //…
St.
  • 521
  • 3
  • 8
8
votes
1 answer

Calling Enum.values() on a generic type

The goal is to implement a generic Enum translator. I know Enum1 and Enum2 have the same values and in the same order. The goal is to implement something like this:(this works) private static Enum1 translateEnum(Enum2 originalEnum) { return…
Gabriel
  • 306
  • 4
  • 16
8
votes
3 answers

generic implementation in C

Hii , While I was implementing some of the programs for the class assignment, it just struck me how it would be to implement the same in a generic fashion using C. I do know that we need to make use of the void pointers and functions but i was just…
Flash
  • 2,901
  • 5
  • 38
  • 55
8
votes
2 answers

Difference between copy_backward and reverse_copy?

I am reading C++ primer and saw these two functions that seem to have the same functionality. Could anyone help and tell me what is the difference between the two? Thanks.
Sean
  • 2,649
  • 3
  • 21
  • 27
8
votes
1 answer

Is it possible to introspect on methods using Boost Hana?

Boost Hana provides the ability to introspect on class member fields in a simple and beautiful way: // define: struct Person { std::string name; int age; }; // below could be done inline, but I prefer not polluting the // declaration of the…
seertaak
  • 1,087
  • 1
  • 9
  • 17
8
votes
1 answer

Generic programming in C

I am writing a generic linked list implementation in pure C. struct Node { void *value; struct Node *next; }; struct LinkedList { struct Node *start; struct Node *end; }; void LinkedList_new(struct LinkedList* llist) { …
collinglass
  • 800
  • 4
  • 11
  • 31
8
votes
1 answer

Length of user-defined string literal as a template argument?

Is there any way to get behavior like this? // Some definition(s) of operator "" _my_str // Some definition of function or macro MY_STR_LEN using T1 = MY_STR_LEN("ape"_my_str); // T1 is std::integral_constant. using T2 =…
aschepler
  • 70,891
  • 9
  • 107
  • 161
8
votes
2 answers

DataTemplate.DataType = Collection?

Is there a way to create a data template that handles a list of items? I have Contact.Phones (EntityCollection) and I want the data template to handle the list - add remove edit etc. Is there a way to set the DataType property of the…
8
votes
1 answer

Deep Copy of a Generic Type in Java

How does deep copies (clones) of generic types T, E work in Java? Is it possible? E oldItem; E newItem = olditem.clone(); // does not work
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117
8
votes
1 answer

programming language where values and objects are different

I am trying to get started with the 'Elements of Programming' by Alex Stepanov and Paul McJones. On Page5 last para: They say, "This book uses a programming language that has no way to describe values and value types as separate from object and…
7
votes
2 answers

D Analogue to C++ member-function-pointers, not necessarily delegates

I have been learning D, and am in particular very excited for it's Generic programming capabilities. Delegates are wonderful, and apparently they have completely replaced member-function-pointers, so I was stuck when I wanted to implement something…
Ramon Zarazua B.
  • 7,195
  • 4
  • 22
  • 26