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

How to define out of class functions for a class specialized using std::enable_if

I have a specialization of a class called graph which is enabled only if the input is a particular type. I am not able to define out of class definitions for the functions inside that class. This question os different from some other questions on…
-1
votes
2 answers

why does Java allow generic array declaration?

I know that array generic array creation is not allowed because arrays need to know their type at run-time but since generic erase their type information at run-time it is not possible to create generic array. But how come it allows generic array…
kaka
  • 597
  • 3
  • 5
  • 16
-1
votes
2 answers

Initialization of multiple structure having same member name & member count in single function

typedef struct { int data; int size; } s1; typedef struct { char data; int size; } s2; typedef struct { float data; char size; } s3; func(void *p) { /*this should be generic to all structure.*/ /* Need to do for removing…
-1
votes
1 answer

Generic Type Return and Argument order in c#

I want to create a generic method like below : Task PerformSomeAction( Request request, string token) where Response : ResponseModel where Request : RequestModel; is…
Jaydeep Shil
  • 1,894
  • 22
  • 21
-1
votes
2 answers

Determining a template type at later point in time C++

I'm looking to try to write some generic code that can handle different data types. Once these data types are set, then it'll remain the same throughout the duration of the instance. I think it'll be easier to show what I'm trying to do, rather than…
SailorCire
  • 548
  • 1
  • 7
  • 24
-1
votes
2 answers

Calling a constructor inside a constructor of a templated class

This code has a templated class. The default constructor appears to call itself recursively. How can it do that? I don't understand this code. Maybe if I would be given an example without templates, just POD types, things would be clearer. I haven't…
Galaxy
  • 2,363
  • 2
  • 25
  • 59
-1
votes
1 answer

Is it a good design to resolve Concrete classes with Generic Interfaces for Dependency Injection?

Is it a good pattern to a have a Generic Interface to implement the Concrete classes and use the generic interface to resolve in container. My concern is does it breaks the Single responsibility principle or is it tightly couples the implementation.…
-1
votes
2 answers

How to print a 1 to N without using semicolon ? Explain this code

// A recursive C program to print all numbers from 1 // to N without semicoolon #include #define N 10 int main(int num) { if (num <= N && printf("%d ", num) && main(num + 1)) { } } How is this program working ?. Please…
-1
votes
1 answer

How can I have an array of Any type?

I'm trying to model a dataframe-like structure. I know how use enums here, but I'm exploring how do it similar to C#/Python/etc. I tried to follow Rust Trait object conversion but things are not working: use std::any::{Any}; use…
mamcx
  • 15,916
  • 26
  • 101
  • 189
-1
votes
4 answers

how can I write generic queries in entity framework?

I have 3 methods these are same methods only some parameters will be change I want to write one method how can i write public string method1(int id) { var getAllStudents = rep.Students.Where(e => e.StudentId == id).ToList(); …
John
  • 23
  • 4
-1
votes
1 answer

Cross-platform endian conversion

I've written this code to convert between host and network endianness and found the implementations for both directions is the same. Is that right? template T be_to_host(T val) { T outval = 0; std::size_t len = sizeof(T); …
-1
votes
4 answers

Asp.net web api linq query generic type

public class Entity : IEntity where T:struct { public T ID { get; set; } public bool IsDeleted { get; set; } } public class EntityRepository : IRepository where T : Entity where Y : struct { public virtual async…
anıl yıldırım
  • 953
  • 1
  • 10
  • 17
-1
votes
2 answers

Get type of Generics

I need to get type of generic type. I've already tried following public abstract class RetrofitRequest extends SpiceRequest> { public RetrofitRequest(){ super(Response.class); //compile time error …
-1
votes
1 answer

What's I should use "where T: "?

What's the diferente between this code? interface ICommandHanddler where T : ICommand { void Handle(T command); } and this code: interface ICommandHanddler { void Handle(ICommand command); } For me it's…
Italo José
  • 1,558
  • 1
  • 17
  • 50
-1
votes
1 answer

How to use the generic function in unity?

I am trying to determine what component is being used as a parameter but I have no clue how to try access it as everything throws an error. This first function AttemptMove detects what the raycast collides with. It then calls the OnCantMove…
Damian
  • 19
  • 6