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

Why would this template not compile?

I have a pretty simple template which is a container which is an array of T. I am getting a syntax error : container.h(7): error C2143: syntax error : missing ';' before '&'. I have tried removing the declaration there but then the error just skips…
Or Cyngiser
  • 1,027
  • 1
  • 12
  • 16
0
votes
1 answer

How do I make my MergeSort generic for different objects?

I currently have a merge sort, which was sorting a list of Nodes according to an integer within each Node called "F" (So Node.F). However, I have come up with a need to use the MergeSort for another list of objects - Entities. However, I want to…
Shivam Malhotra
  • 309
  • 1
  • 3
  • 10
0
votes
1 answer

Create instance of generic type in Java within the generic class

According to the question, Create instance of generic type in Java? At the time of writing ,the best answer is found to be... private static class SomeContainer { E createContents(Class clazz) { return clazz.newInstance(); …
johnkarka
  • 365
  • 2
  • 14
0
votes
1 answer

Generic Type x Generic Parameter: Building a "very generic" structure

AIM For instance, we have a Tree class of type T: i.e. Tree. We would like to make this Tree class to be able to hold Tree (of course), SubTree where SubTree extends Tree, Tree where SubT extends T, and SubTree where SubTree…
midnite
  • 5,157
  • 7
  • 38
  • 52
0
votes
1 answer

Better programming - generic or multiple code in methods

I am wonder, you have some part of code you need to do in, for example, every method. I know everyone have another "style" of programming and everyone prefer another ways how to do something. But for example i am writing now some adapter on some…
tomdelahaba
  • 948
  • 3
  • 13
  • 26
0
votes
2 answers

Checking for list membership using the STL and a unary function adapted functor

I've attempted to write a brief utility functor that takes two std::pair items and tests for their equality, but disregarding the ordering of the elements. Additionally (and this is where I run into trouble) I've written a function to take a…
Shamster
  • 2,092
  • 5
  • 24
  • 27
0
votes
3 answers

How to update Non-Generic code to make it Generic?

Currently ,Managing few algorithms which perform set of normal operations on set of Student object. I wanted to make these algorithm generic , so that we can perform those operations on others Object like Student. I see in my legacy code base,…
Sanjiv
  • 1,795
  • 1
  • 29
  • 45
0
votes
3 answers

C++ Losing Template Data

I don't consider myself all that knowledgeable in C++ but I'm having a hard time with this concept. So I have a class the holds some template datatype and a double. I want the m_data variable to be generic, but right now I'm only testing with an…
Odub
  • 33
  • 2
  • 9
0
votes
2 answers

Strange parameterized method

public class StrangeParamMethod { static void f(ArrayList list){}; public static void main(String... args){ ArrayList list = new ArrayListGenerator().list(); //assigns without problems f(new…
0
votes
1 answer

Generating lambda expression - Type conversion Error

I have a generic method that filters a list of entities, filtering is done by generating lambda expressions: protected object initFilters(string targetEntity, List searchItems, int page_size = 20, int offset = 0 ,…
SirZoro
  • 151
  • 1
  • 2
  • 10
0
votes
1 answer

how to create a program that generate a source code programatically and compile it and execute after successfull compilation

How to create a program (Parent Program) that creates .cs(generate source code programatically from a Parent Program) file and compile it and execute after successfull compilation and communicate result with Parent program. What i done is Step 1: i…
Civa
  • 2,058
  • 2
  • 18
  • 30
0
votes
1 answer

Nesting generic parameters

Is possible to nest generic parameters like class MyClass>{} ? I tried some ways and got the following code that works, but the class declaration needs to receive the two parameters separated: class MyClass1 where TM : MyType …
Luciano
  • 2,695
  • 6
  • 38
  • 53
0
votes
4 answers

Attaching an event handler to a generic type initialized using reflection at runtime with unknown type parameter

Please have a look at code below, which is based on the assumption that you have a controller class Controller. It is a generic class with constraint CGeneric where T:IRecord, two concrete record classes CRecordCustomer:IRecord, and…
moral
  • 150
  • 1
  • 6
0
votes
3 answers

Switch according to input and return dynamic value

In this method I get string as input and according to the string name I need to return value sometimes its string sometime int ,double,int64 ,bool etc Since its dynamic type i don't know how to define it in the method return type and how to add to…
0
votes
2 answers

Ideas to implement generic web service

I'm thinking of a mid / large scale project, which will need to store different data types and present them to different clients. What I'm struggling now is, how to build a data and service layer that can capable of storing different types of…
Tequilalime
  • 611
  • 9
  • 29