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
7
votes
3 answers

Which way is better for creating type-agnostic structures in C?

I'm trying to write some generic structures. Essentially, what I need for my purpose is C++ templates, but since I'm writing in C, templates are out of consideration. Currently I'm considering 2 ways of achieving what I want. Method 1: use the…
user500944
7
votes
2 answers

What is the difference between std::invocable and std::regular_invocable concepts?

What is the difference between std::invocable and std::regular_invocable? Based on the description from https://en.cppreference.com/w/cpp/concepts/invocable I would expect that the std::regular_invocable concept doesn't allow to change the state of…
7
votes
2 answers

Calling method of same name from different class

I have some classes those have methods with same name. For example public class People { private Long id; private String nm; private String nmBn; ............. public Long getId() { return id; } public String…
Md. Shougat Hossain
  • 501
  • 2
  • 8
  • 24
7
votes
2 answers

Multiple definition of function in the same place

I am trying to simulate generics in C by having some preprocessor definitions for a matrix type. Here is an excerpt of that: #define __matrix_struct(TYPE) \ struct { \ uint32_t sz; \ TYPE **ptr; \ } #define __matrix_t(TYPE) matrix_ ##…
Victor
  • 13,914
  • 19
  • 78
  • 147
7
votes
1 answer

Template specialization for fundamental types

Is there any way to make a template specialization for fundamental types only? I have tried to do the following: template::value>::type> class foo { } template
Andreas Loanjoe
  • 2,205
  • 10
  • 26
7
votes
4 answers

How to express "the minimum integral type larger than T"?

Suppose I have an integer type T (signed or unsigned). I want to refer (at compile time) to the smallest integer type (signed or unsigned) which can hold, say, std::numeric_limits::max() plus 1 (in the non-overflowing sense, I mean). What's a…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
2 answers

void return value from a function used as input to a templated function is seen as a parameter

Say you have some target class with some methods on it: class Subject { public: void voidReturn() { std::cout<<__FUNCTION__<
Skillcheck
  • 151
  • 1
  • 6
7
votes
2 answers

Automatically derived sealed trait/ADT ordering in Scala

Is it possible to automatically derive order on a sealed trait family in Scala? For instance, it would be nice to be able to do: sealed trait Letters case object A extends Letters case object B extends Letters (A < B) == True This feels like…
Noel M
  • 15,812
  • 8
  • 39
  • 47
7
votes
1 answer

How do i check a type against its parent class?

Simple cases of, int being a number really. Im creating a generic class test and i want to test T to see if its inherited from number (or num in the case of Dart). Based on the Objects, Object > num > int in the docs, so at first I was…
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
7
votes
2 answers

template template total specialization

A template template specification is like this: template < template < class > class T > struct MyTemplate { }; How am I supposed to create a total (or partial) specialization for this template? Is this possible?
scooterman
  • 1,336
  • 2
  • 17
  • 36
7
votes
1 answer

C++ about generic initialization in templates

I am writing a generic function like below. template void foo(Iterator first, Iterator last) { T a; cout << a << endl; // do something with iterators } typedef vector::iterator DblPtr; vector
7
votes
3 answers

How to make some generic programming in fortran 90/95 working with intrinsic types

I would like to program some procedure that will work with different types. I am planning to use the "include" method used in flibs described here and here. I give here a simple exemple. !…
janou195
  • 1,175
  • 2
  • 10
  • 25
7
votes
2 answers

Scala: Implementing a subtype of Numeric[T]

How does one go about implementing a subtype of Numeric[T]? I have been looking for at guide on this but haven't found any. Example of subtypes could be Rational or Complex? Thanks in advance Troels
Troels Blum
  • 823
  • 8
  • 17
7
votes
2 answers

Calling methods common to types in a boost::variant

If all the types in my boost::variant support the same method, is there a way to call it generically (i.e. not calling it seperately for each method of the static_visitor)? I'm trying to get something like this to work: class A { void boo()…
thehouse
  • 7,957
  • 7
  • 33
  • 32
6
votes
1 answer

Flutter create Generic Function with T extending an interface

Context In my code, I have an interface called AbstactDataModel that is used as a starting point for all the data model classes. This is implemented so that i know that whatever xyzModel class I may need, it will have a fromJson(Map
Mike
  • 135
  • 1
  • 9