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

How to typecaste control dynamically?

C# .net , winforms application., Visual studio 2010 I have created a control c which can be Textbox or combobox. now i want to type cast c dynamically that if sender is combobox than typecast c with combobox, if sender is textbox make c as…
yogeshkmrsoni
  • 133
  • 1
  • 1
  • 8
0
votes
1 answer

"int" to "generic" data type gives errors in c#

I am c# beginner and created huffman tree which works for "int" type symbol but next step is to make it generic.In explain this symbolshould work for every data type.By every data type i mean symbol could be of type int, ulong,short etc. Actually i…
Sss
  • 1,519
  • 8
  • 37
  • 67
0
votes
2 answers

Tree creation using generics in c#

I am new to c# i have just completed a huffman tree and now next step is to make it generic i mean this symbolshould work for every data type. Since i am c# beginner i need some basic idea to do that. My huffman tree consists of 3 classes. Class…
Sss
  • 1,519
  • 8
  • 37
  • 67
0
votes
2 answers

Why does this Fortran module interface give different results depending on how many of its functions are used?

I have written a module that contains a interface called 'push' that pushes values onto allocatable arrays. I want it to have generic behavior so that I can add a new function for a given type to the 'push' interface as needed. The problem is that…
0
votes
1 answer

How do I differentiate between a char[3] and a string literal with three characters?

If I call this function: template size_t Foo(const T& str) { return std::distance(std::begin(str), std::end(str)); } ... with these three types: const auto a = "abc"; const char b[3] = {'a', 'b', 'c'}; const std::string…
Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90
0
votes
2 answers

Can a generic function be used for summing structs?

Suppose I have a struct, such as: struct Foo { double a; float b; int c; }; Summing fooA and fooB (both of type Foo) would equate to: {fooA.a + fooB.a, fooA.b + fooB.b, fooA.c + fooB.c}; Now my question is, could a generic function…
Fault
  • 420
  • 3
  • 17
0
votes
0 answers

Starting with Python and GUIs

A friend of mine encouraged me to start programming in Python to do "lots" of things in a kinda "easier" way. I already have a base of C. I'd like to start doing some GUI stuff in python (even if I just started learning it) just to do something…
0
votes
2 answers

How can I store a generic tree in a file?

I have a generic tree implementation written in C++ that can store any type (int, float, string, etc.) template class Tree { public: // Class interface private: T node; std::list*>* children; }; and I have a program (written in…
WileTheCoyot
  • 513
  • 1
  • 5
  • 20
0
votes
2 answers

Add two generic types into another type

In my code i have (E and V are generic types): E info; V v1, v1; If i write: info = (E) v1 + v2 I get an error: SparseGraph.java:163: operator + cannot be applied to V,V. How can i add v1 and v2 to get the result inside the "info" variable? As…
Marco Micheli
  • 707
  • 3
  • 8
  • 26
0
votes
2 answers

How to define a variable which can be used to store both a pointer-to-int and iterator-to-int-container?

What is an appropriate type for type T below? I don't want to use auto because I want to create a class with a member variable of type T. Thanks. int* ptr; vector vec; T var = ptr; T var2 = vec.begin(); T var3 = vec.end(); var++; var2++; …
Madhav Jha
  • 863
  • 1
  • 8
  • 26
0
votes
0 answers

best-practice for #ifdef around VS2012 versus VS2013 for c++11 support?

What's a good/best way to toggle some function declarations based on VS version? context: I need to build on linux and windows and keep hitting cases where GCC compiles fine but VS2012 lacks some c++11 features. I'd like to #ifdef out the…
0
votes
1 answer

returning a std::function wrapped lambda that invokes a specified pointer to member function

I have a case where I can pass a lambda to std::sort, and I can also supply the predicate by calling a function that returns a std::function which wraps this same lambda, but, if I try to call a similar function which allows me to specify a pointer…
0
votes
3 answers

Freaky output: why would this code give any meaningful output, let alone this?

I'm not sure how to even state my question, but here we go... So, I have this class for which operator[] has an empty body (not yet implemented). Still, when I call it from main(), it produces an output. What's more, the output is exactly what was…
0
votes
3 answers

generic programming, generic data structures

I am trying to implement a Binary Search Tree and if I use the generic programming in java then this tree should be able to store any kind of data e.g. int, Strings or basically any other object. But the problem with such a class is with coding the…
Ankit Goel
  • 6,257
  • 4
  • 36
  • 48
0
votes
3 answers

permutations of a list java

I am trying to generate all possible permutations of a given list in a generic function. I think I have the function code correct. However, during testing time I keep getting compiler errors from my main program. Please help! import…
RuneScape
  • 75
  • 1
  • 7