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

Dart, Can't call Generic's method

I am trying to create an abstract data model where i pass data and type a and then is return list, but when i can't call T.fromJson() method, note that passes type has method fromJson() class DataList { final bool success; dynamic data; …
Tornike Kurdadze
  • 955
  • 2
  • 10
  • 24
16
votes
3 answers

Adding a custom view to XML... but with a GENERIC-type

I am working on a custom view with a hope of reusability. It should have a generic type, like this: public class CustomViewFlipper extends ViewFlipper { } I know how to bind a normal custom view to the XML file. But I couldn't find any…
eks
  • 501
  • 4
  • 13
15
votes
12 answers

Where do you find templates useful?

At my workplace, we tend to use iostream, string, vector, map, and the odd algorithm or two. We haven't actually found many situations where template techniques were a best solution to a problem. What I am looking for here are ideas, and…
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
15
votes
3 answers

The way to instantiate map> in Java

I would like to instantiate Map> in Java, I tried Map> foo = new >(); and Map> foo = new >(); None of them work. Does any one know how…
Alfred Zhong
  • 6,773
  • 11
  • 47
  • 59
14
votes
5 answers

Inheritance & virtual functions Vs Generic Programming

I need to Understand that whether really Inheritance & virtual functions not necessary in C++ and one can achieve everything using Generic programming. This came from Alexander Stepanov and Lecture I was watching is Alexander Stepanov: STL and Its…
Avinash
  • 12,851
  • 32
  • 116
  • 186
14
votes
1 answer

Why is Overload Resolution favoring unconstrained template function over a more specific one?

I have this minimal expression template library with a multiplication, i.e. template struct mul { const T &v1; const U &v2; }; template mul operator*(const T &one, const U &two) { …
Nibor
  • 1,236
  • 9
  • 23
13
votes
2 answers

Folding over a polymorphic list in Haskell

I have a collection of records spread across a number of types in a large Haskell application that reference each other. All of the types involved implement a common typeclass. The typeclass contains functions that work over a variable and all of…
13
votes
7 answers

How to implement a generic macro in C?

FUNC(param); When param is char *,dispatch to func_string. when it's int,dispatch to func_int I think there may be a solution to this,as variable types are known at compile time..
lexer
  • 1,027
  • 3
  • 14
  • 18
13
votes
5 answers

Generic/template programming best practices: To limit types, or not to limit types

That is my question. I'm just curious what the consensus is on limiting the types that can be passed in to a generic function or class. I thought I had read at some point, that if you're doing generic programming, it was generally better to leave…
Brett Rossier
  • 3,420
  • 3
  • 27
  • 36
13
votes
1 answer

expected primary-expression before ‘>’ token

I have a code like: class Client2ServerProtocol { }; class ProtocolHelper { public: template int GetProtocolId() { return -1; } }; template<> inline…
ejkoy
  • 197
  • 1
  • 1
  • 8
13
votes
1 answer

C++: Why decltype (*this) returns a reference?

template struct foo{ void f(){ decltype(*this) a(*this); do_some_test(a); } T data; }; //compiler won't accept this In my interpretation, decltype should return the a type so that we can use it in…
sqd
  • 1,485
  • 13
  • 23
13
votes
2 answers

Safely copying fields between case classes of different types

Assuming you have case classes like the following case class Test1(a:String,b:Int,c:Char) case class Test2(a:String,b:Int) And you instantiate the classes with the following variables val test1 = Test1("first",2,'3') val test2 =…
mdedetrich
  • 1,899
  • 1
  • 18
  • 29
12
votes
1 answer

Handle variadic templates for general-case mixed types and non-types

So I'm trying to make a type trait that says whether two "outer" class types are the same. ie. std::vector is the same as std::vector, I don't care about any inner arguments for my type trait. A problem that I had with trying to make a…
12
votes
1 answer

How to write generic function with two inputs?

I am a newbee in programming, and I run into an issue with R about generic function: how to write it when there are multiple inputs? For an easy example, for dataset and function z <- c(2,3,4,5,8) calc.simp <- function(a,x){a*x+8} # Test the…
12
votes
4 answers

How can I pass a “Type” as a argument to function in c?

I want to write generic function (for example, function that get array in type of void** and doing something about this array) such then this function will get the type of the element (in the example, it's will be the type of any element in the…
AskMath
  • 415
  • 1
  • 4
  • 11
1 2
3
85 86