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

c++ call template constructor to instantiate

Give the following program, hello.h #ifndef HELLO_ #define HELLO_ template class hello { T _data; public: hello(T t) : _data(t) {} }; template class world : public hello { T _data; public: world(T t) :…
4af2e9eb6
  • 682
  • 3
  • 7
  • 20
-1
votes
1 answer

Output for pass-by-value and pass-by-name

I am needing to find the result of the following code when x and y are passed-by-value and also when passed-by-name. PROGRAM EX1; int i; //global int A[3]; //global PROCEDURE P1(int x, int y) Begin y:=2; PRINT(x); …
-1
votes
2 answers

'domain_error' in namespace 'std' does not name a type

I am trying to implement a generic matrix and I got an errors on each decleration of domain_error in my code.. error: Matrix.h:31:60: error: 'domain_error' in namespace 'std' does not name a type Matrix operator+(const Matrix &other) const…
-1
votes
1 answer

C# generic T function where T is from a string

I'm creating a synchronize function between a device and server for a large database. I have a lot of listing tables (the items in a dropdown/picker). I don't want to write a lot of code and I'm looking for an elegant solution :) On a device in…
Enrico
  • 3,592
  • 6
  • 45
  • 102
-1
votes
4 answers

How to return the subclasses of generic type

I have this classes: public abstract class Parent {} public class Child extends Parent{} I made the class Connector where i want that T type will assume the subclasses of Parent: public class Connector { T t; public…
joumvaer92
  • 252
  • 3
  • 14
-1
votes
1 answer

C - generic function: swap two items in array

My problem: I would like to create function that can swap any two items in array of generic type. I have SwapG function that can swap two items of any type: void SwapG(void * a, void * b, size_t size) { void * temp = malloc(size); …
Erik Cupal
  • 2,735
  • 1
  • 19
  • 20
-1
votes
2 answers

Passing a double to a method that accepts T

Question I am trying to pass a double to a method that accepts T. That method (In this case. I plan to add the cases for String, int, etc.) requires the item passed to be a double or else it will throw an error. Basically, I want to pass a double to…
Chris
  • 2,435
  • 6
  • 26
  • 49
-1
votes
4 answers

Is it possible to use generic programming without templates?

Say for example I have a function that returns a value from a 2-dimensional array: float get_2d_noise(const point& p) { return map2D[p.x][p.y]; } The point is a class that I've defined as part of my library: struct point { int x; int…
NeomerArcana
  • 1,978
  • 3
  • 23
  • 50
-1
votes
2 answers

Is there another simple way to do with c++ properties?

I've tried to use c++ properties and now I'm stuck with this: class a { protected: // wikipedia https://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B template class getonly { …
Lê Tuấn
  • 13
  • 3
-1
votes
1 answer

How to properly declare a return type of a future subclass type of a related generic base class?

Someone is trying to achieve an abstraction in ASP.NET MVC with C#. They have a base entity controller which should do most of the job for its derived classes(entities). In it, they have a problematic method which should return the database context…
Tyree Jackson
  • 2,588
  • 16
  • 22
-1
votes
2 answers

How to use a generic editor for database access in tapestry 5?

I have a tapestry 5 project that contains the following: An abstract entity in the entities package that is inherited by all the other concrete entities import java.io.Serializable; import javax.persistence.Basic; import…
-1
votes
1 answer

How to use Automapper inside GenericRepository to Map Entities to DTO?

I want to add a method to my Repository that maps the given Generic type to a DTO. Assume that these are my Entity and DTO: public class User : BaseEntity { public string firstName { get; set; } public string lastName { get; set; } …
Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71
-1
votes
2 answers

Generic class and interface in c#

How can I do this in C#? public class SomeClass{} This is a generic class of T, and T must implement the interface SomeInterface.
monther zlatan
  • 156
  • 1
  • 14
-1
votes
1 answer

Generic method that extends Comparable and gets a Generic Data Structure type

I have a generic class that implements a "Set" using ArrayList: public class MySet { ArrayList setList; public MySet(){ this.setList = new ArrayList(); } public MySet(E[] inputArray){ this(); for(E…
Yevgeni
  • 1,533
  • 17
  • 32
-1
votes
2 answers

how to know if cpu is running in 64 bit in linux?

I'm looking for a way to know if the cpu of my linux system is running in 64 bit mode. I've got to programm it for the university. My idea was to create something like an function that provides me that information. I'm not sure how should I start.…