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

Do I need to specify typename in the return type of member function if the function is defined inside the body of the class?

Consider the example below: template class SomeClass { // rest of the definition ... SomeClass& function1() { // ... return *this; } SomeClass& function2() { // ... return *this; …
user5818995
4
votes
2 answers

How to make return type of function generic in Swift

Intro In my app, I have one superclass called "ElementData" and several child classes that inherit from it. Each child class has its own validateModel() method which returns a different type, depending on the class - always in an Array. In other…
linus_hologram
  • 1,595
  • 13
  • 38
4
votes
1 answer

Generic usage of Diesel's find or filter to perform deletions

I'm trying to use generic Diesel functions to shrink repetitive tasks like deleting a row based on the primary key. I got generic insertion of rows working relatively quick, but deletion queries seem to be quite hard. I tried solving it both by…
4
votes
2 answers

Identifying Differences Efficiently

Every day, we receive huge files from various vendors in different formats (CSV, XML, custom) which we need to upload into a database for further processing. The problem is that these vendors will send the full dump of their data and not just the…
SRaj
  • 1,168
  • 1
  • 14
  • 36
4
votes
2 answers

Using a template class in STL containers

If I have a template class, that I want to instantiate with different data types: template class A { T value; // ... }; And I also want to use the objects of this class in a Standard Template Library container (say vector). In my…
lulijeta
  • 900
  • 1
  • 9
  • 19
4
votes
2 answers

Variadic template for multidimensional std::array

We can alias a multidimensional array like this: template using myArray = std::array, size1>; But this only allows us a predefined number of dimensions. Is there a way to turn this into a…
Michael Mahn
  • 737
  • 4
  • 11
4
votes
3 answers

Given a number K and a set of sorted numbers. Find if there is any number in the set which divides

Given a number k and a set of sorted numbers. Find if there is any number in the set which divides this number. For example if k = 8, and set is { 3, 4, 5}, 4 will divide 8. 4 is the answer. Worst case solution is O(n). Can we do it better?
sunmoon
  • 1,448
  • 1
  • 15
  • 27
4
votes
3 answers

How to implement Generic Kafka Streams Deserializer

I like Kafka, but hate having to write lots of serializers/deserializers, so I tried to create a GenericDeserializer that could deserialize a generic type T. Here's my attempt: class GenericDeserializer< T > implements Deserializer< T > { …
Mark Lavin
  • 1,002
  • 1
  • 15
  • 30
4
votes
3 answers

Conditionally implement a Rust trait only if a type constraint is satisfied

I have the following struct: pub struct Foo { some_value: T, } impl Foo { pub fn new(value: T) -> Self { Self { some_value: value } } } // Implement `Default()`, assuming that the underlying stored type // `T` also…
Donald Whyte
  • 195
  • 2
  • 9
4
votes
1 answer

Casting By Generics

Is there a way to cast an object to return Generic value of a method? I tried this way but to happen Casting exception: public S get(Class clazz) { return (S) new Super(); } public class Super { } public class Sub1…
ImanX
  • 789
  • 6
  • 23
4
votes
1 answer

Generic Classes With Protocols Swift

I have a basic protocol like this: public protocol BasicSwiftClassLayout { var nibName: String { get } } and my class is this: public class BasicSwiftClass {} I've defined 2 different structs so that I could…
Logan
  • 1,172
  • 9
  • 23
4
votes
4 answers

Moving in range-based loop in generic C++ code?

Imagine that you have this generic pseudo-code: template void f(Iterable&& iterable) { ... } We want to handle rvalue and lvalue references to iterable objects1, and the idea is that the function handles the container…
dodomorandi
  • 1,143
  • 1
  • 11
  • 18
4
votes
2 answers

How to enforce a formal protocol with C++ templates?

When using the compile-time duck typing inherent with the template style, is there any way to enforce the requirement that the template argument implements certain methods with certain signatures? struct ProtocolT { void g() const; void…
max
  • 49,282
  • 56
  • 208
  • 355
4
votes
1 answer

Does this higher order function have a name?

I see this pattern everywhere in my code, and in libraries, yet there appears to be no name or abstraction of it that I can find anywhere. Example (pseudocode) T foo( T x, void f(T&) ) { T y = x; f( y ); return y; } Basically: Take a…
4
votes
6 answers

C++ generic class dealing with dereferencing syntax

What is the best way to deal with the fact that some types require members / methods to be accessed with the . operator whilst others with the -> operator. Is it best to write the code for the . operator and have the caller wrap the type as show in…
Blair Davidson
  • 901
  • 12
  • 35