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

C++ functional & generic programming [with MySQL connector example]

I am going to use MySQL connector. They provide functions to access the result row. Some examples are getString(1), getInt(1), getDate(2). The number inside the parenthesis is about the index of the result. So that I have to use the following code…
HKTonyLee
  • 3,111
  • 23
  • 34
0
votes
2 answers

Defining a custom iterator traits for a generic function

I'm trying to write a generic function which will derive a return type at compile time according to the iterator it is given. Usually this is done through std::iterator_traits, but I also wanted to define my own version of iterator_traits, called…
Drew Cheerio
  • 13
  • 1
  • 3
0
votes
1 answer

To get datatype of list or Collection as method param using reflection

I have to create a method that has body something like below: public void anyMethod(List dataList){ for (Iterator it = dataList.iterator(); it.hasNext();) { Object p = it.next(); if(p instanceOf XYZ){ …
user3363969
  • 233
  • 1
  • 4
  • 15
0
votes
1 answer

Access to DB from base model

I have a basemodel as below, and i am using this model to get/set created and modified date per model. public class BaseModel { [ForeignKey("CrUser")] public ApplicationUser UserCr { get; set; } public string CrUser {…
umki
  • 769
  • 13
  • 31
0
votes
3 answers

Generic function for calling back every kinds of methods with different arguments in C#

I want to have a function which is able to call any function and retrieve a proper value. these are some examples of what I need: var IList = InvokeFunction>(repositoryPerson.GetAllPerson); var Persion =…
0
votes
1 answer

c++ generic programming with templates and nullptr

Let's say that I have this generic function: template void foo(T data) { if(data == nullptr) return; //... } The problem is that I can not really write something like that. If T is a primitive type or an object passed by value, I…
Kami
  • 1,079
  • 2
  • 13
  • 28
0
votes
1 answer

How to use C++ std::sets as building blocks of a class?

I need a data structure that satisfies the following: stores an arbitrary number of elements, where each element is described by 10 numeric metrics allows fast (log n) search of elements by any of the metrics allows fast (log n) insertion of new…
0
votes
3 answers

Real-world examples with subtyping constraints in .NET generics

Are there any real-world examples of using subtyping constraints on type parameters in .NET generics? By «subtyping constraints» I mean where T : and where T : U May be there are some standard generics with corresponding…
juliet
  • 193
  • 6
0
votes
3 answers

How to access class members using void pointer(generic) in c++

code:I want to access class members function display using generic ptr p...how should i do? #include "stdafx.h" #include using namespace std; class car { public: int i,j; car():i(5),j(9){}; car(int a,int b):i(a),j(b){} void…
0
votes
2 answers

Delegate for generic method return

I have a method - public T GetField (string tableName, string fieldName) { //Code } I need a delegate to hold this method. Not sure how to declare delegate for above method. Now if I change my method public T GetField (string tableName,…
Ranjan Kumar
  • 497
  • 2
  • 8
  • 24
0
votes
1 answer

How to use a DbSet with a generic type

I have an interface: public interface ILanguageEntity { int Id { get; set; } string Name { get; set; } string Code { get; set; } string Culture { get; set; } string LocalName { get; set; } bool IsRightToLeft { get; set;…
Saeed Hamed
  • 732
  • 2
  • 10
  • 28
0
votes
3 answers

C++ Function Nested Templates

I would like to write a function that can receive both QList and QVector of any type: QList iList; QVector iVector; QList dList; QVector dVector; all these types must support invocation my_func(iList); my_func(iVector);…
tmporaries
  • 1,523
  • 8
  • 25
  • 39
0
votes
3 answers

Is It A Generic Stack Data Structure Linked List Implementation in C?

My college professor taught us that a generic stack looks something like this (I basically copy-pasted this from the course support files): typedef struct { size_t maxe, dime; char *b, *sv, *vf; } TStiva, *ASt; #define DIME(a)…
Dragos Rizescu
  • 3,380
  • 5
  • 31
  • 42
0
votes
2 answers

Can C++'s value_type be extended from iterator_traits to all types?

I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly for all types using the same syntax. Imagine we have the following: template struct value_type { typedef T type; }; #define…
Mark Ruzon
  • 3,297
  • 2
  • 15
  • 11
0
votes
1 answer

Generic type in C# not working on reading full binary file (Unhandled exception)

I am trying to read a binary file taken at sole argument and i have just switched to c# from c++. I know what the problem is: The problem is when i read the binary file it comes byte by byte (8 bits) and that i save into a variable (this variable…
Sss
  • 1,519
  • 8
  • 37
  • 67