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

Generic functions taking unknown type parameters in C

I'm trying to make some functions, taking unknown type parameters, to generically apply functions. Let's take for example a function that could apply close(int fd) for each element of an array: void for_each(void *array[], void (*func)(void *)) { …
BiasInput
  • 654
  • 1
  • 10
  • 28
5
votes
1 answer

How does Java handle ambiguous type inference for generics?

In this code, T can be A, B, C, or D, but Eclipse shows that it is D. static class A { } static class B extends A { } static class C extends B { } static class D extends C { } static void copy(List dst, List src) { …
5
votes
1 answer

How to make a generic class conform to a protocol for specific type?

Say there exists a generic struct: public struct Matrix where T: FloatingPoint, T: ExpressibleByFloatLiteral { // some methods... } Is it possible extend the struct to conform to a protocol for constrained T using where clauses? E.g. something…
5
votes
1 answer

Generic OpenCL stencil kernel and host

I am new to OpenCL. I would like to write a generic kernel so later I can extend its use to other memory non-coalescing patterns and pairing this with Rectangular stencil pattern for simplicity (also avoiding out-of-bound access). This kernel…
Amir
  • 1,348
  • 3
  • 21
  • 44
5
votes
1 answer

Getting the associated Output type of an Add implementation given LHS and RHS types

In Rust, is there any way to, at the type level, summon an Add implementation by using the LHS (Self) and RHS types in order to use its Output type (in say, the return type of a generic function)?
lloydmeta
  • 1,289
  • 1
  • 15
  • 25
5
votes
4 answers

AS3 Remove element from array (of objects) generically

Is there a way to generically remove an object from an array? (maybe not using array.filter or creating a new array) Example: var arr:Array= new Array(); //create dummy objs for (var i:uint=0; i < 10; i++){ var…
Amitd
  • 4,769
  • 8
  • 56
  • 82
5
votes
1 answer

c++ variadic template argument iterating

I'm pretty inexperienced in such things, but I'm trying to create a template function that evaluates a n-variable function at "rotated" argument (see example below) and returns a vector of all these values. For example for n=3 with a function…
5
votes
1 answer

Amazon API Gateway Import From Swagger Error - Not taking Generics

I'm trying to create new APIGateway via import from Swagger, but having validation errors: The particular class causing the issue is our PaginationModel class. Code model definition: public class PaginationModel { public IEnumerable items…
5
votes
1 answer

Linq expression IEnumerable does not contain definition of where

How to write correct Linq expression used in generic for condition "where" public static class ConStr { public static MySqlConnection Conn() { return new…
jake talledo
  • 610
  • 11
  • 24
5
votes
2 answers

Composable C++ Function Decorators

Python has a very useful feature of function decorators, which, moreover, allows composition. For example, if write a function foo, then you can state that you would like foo to be memoized, but also retried more than a single time in case of a…
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
5
votes
1 answer

Why differentiate predicate and non-predicate versions for generic algorithms?

The standard library does differentiate predicate and non-predicate versions of generic algorithms. For example, std::sort() looks like: template< class RandomIt > void sort( RandomIt first, RandomIt last ); template< class RandomIt, class Compare…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
5
votes
4 answers

Confused about the pointers and generic(void) pointers in C

I missed a couple classes, and don't really understand the flowing lecture slide's examples about the void pointer. In the line "no,no,no", why we cannot deference P since P has been assigned a real pointer type q? In the line " ?? ", is it…
overloading
  • 145
  • 9
5
votes
1 answer

How to derive Additive generically on Haskell, without defining an Applicative instance?

Given a type, there is only one obvious way to implement an Additive instance, from the Linear library, to it. Conveniently, Additive has a generic implementation, so we can use deriving for it. Unfortunately, it depends on the existence of an…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
5
votes
2 answers

Function templates for arbitrary STL containers containing arbitrary types

I have an arbitrary STL container C, which contains elements of an arbitrary type T. I want to create an std::vector that has a copy of all the elements. What is the cleanest way to do this? template void myfunction(C container){ …
Chad Brewbaker
  • 2,523
  • 2
  • 19
  • 26
5
votes
3 answers

How to make Parameters of VB.NET function as Generic type?

I have a VB.NET function as below, the parameter 'x' that is passed to the function is of Type 'Single'. However, I want to write the function so that it can accept any numeric type such as 'Single', 'Double' and 'Integer'. I know one way of doing…
N.T.C
  • 658
  • 2
  • 9
  • 20