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

Typed Racket: Creating generic types with define-type

I'm trying to get a bit into Typed Racket, but I'm having some trouble getting an (admittedly rather constructed) experiment to work. This is what I originally had: #lang typed/racket (: generate-list (All (A) ((A -> A) (Integer -> A) Integer…
cronotk
  • 147
  • 10
4
votes
3 answers

templates problem ('typename' as not template function parameter)

Actually I've a problem with compiling some library with intel compiler. This same library has been compiled properly with g++. Problem is caused by templates. What I'd like to understand is the declaration of **typename** as not template function…
bua
  • 4,761
  • 1
  • 26
  • 32
4
votes
1 answer

Check in reflection if type is primitive or type is object

Im using the following code to find class members in reflection that are primitive and some object ,my question is there is a way to identify if field is type primitive ,object ,class reference because i want to call to specific method according to…
John Jerrby
  • 1,683
  • 7
  • 31
  • 68
3
votes
1 answer

Calling static function template within dependent scope

Suppose I have a static function template template void ft() within a struct template template S, and I want to call ft from another function template template void g(), passing the bool template parameter from g to…
Ose
  • 726
  • 7
  • 13
3
votes
2 answers

Implementing reflection: How to find a parent of a class

I've got kind of a challange here. I'm working on a reflection library as both an exercise and utility kit for future projects. To make it work, I already accepted that it's inevitable to be intrusive and I will need to add at least a tiny bit of…
Andrew
  • 157
  • 9
3
votes
2 answers

A generic Functor class in C++

I was trying to built atop the thread here: Variable length template arguments list? to have a default Functor class, this is only of academic interest. My goal is to build a generic Fucntor class: given a class name, method name and argument…
user1186270
  • 33
  • 1
  • 4
3
votes
1 answer

Error calling template method in "templated-base-class"

The following code does not compile, why is that? And how can I fix this? struct A{ template int get() { return N; } }; template struct B : public X { template int get() { return X::get(); } }; int…
Allan
  • 4,562
  • 8
  • 38
  • 59
3
votes
3 answers

Using tags in function overloading

Why is method test in X ambiguous, and how can this be fixed? struct A{}; struct B{}; template struct I { void test(T){} }; struct X : public I, I {}; int main(int argc, const char *argv[]) { X x; x.test(A()); return…
Allan
  • 4,562
  • 8
  • 38
  • 59
3
votes
2 answers

Why can't I put an object of superclass of B into Container?

I have the code below. It seems that I can't put an object of class Nonlife that is a superclass of class Vehicle into a container of type Collection ALTHOUGH there is a keyword "super" in the wildcard type, and only objects of…
cmpltrtok
  • 323
  • 3
  • 9
3
votes
1 answer

Doesn't using directives work with c++20 concepts?

Does using directives not work with concepts? why? The example below doesn't work and I get a compiler error saying that it expected a type. #include namespace A::X { struct BaseA {}; template < typename AType > concept…
yasht
  • 195
  • 1
  • 15
3
votes
4 answers

C - Populate a generic struct inside a function without malloc

I'm trying to build a generic function that can populate a struct without any dynamic memory allocation. The following code is a naive example of what I'm trying to do. This code will not compile as incomplete type 'void' is not assignable. Please…
gberth
  • 467
  • 5
  • 13
3
votes
0 answers

Kind polymorphism in closed type families

I was trying to implement the type family Ty form the paper Generic Programming of all Kinds which computes the type of an Atom. I first tried to use a closed type family just like what paper did: {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs…
Poscat
  • 565
  • 3
  • 15
3
votes
1 answer

Is it possible to define _Generic's association-list dynamically?

I have a template like this: template.h ---------- // Declare a function "func_type()" void JOIN(func_, T)(T t) { return; } #undef T which I use like this in order to generate the same function for different types: example.c --------- #define T…
grep9090
  • 43
  • 5
3
votes
1 answer

How to combine @singledispatch and @lru_cache?

I have a Python single-dispatch generic function like this: @singledispatch def cluster(documents, n_clusters=8, min_docs=None, depth=2): ... It is overloaded like this: @cluster.register(QuerySet) @lru_cache(maxsize=512) def _(documents, *args,…
Carsten
  • 1,912
  • 1
  • 28
  • 55
3
votes
2 answers

How to use enable if with template arguments and parameter pack?

I was working around with c++ templates and came across this piece of code which is using SFINAE using std::enable_if. I am facing two issues with this code. #include #include enum class Type : int { First = 1, Second, Third…