generic-programming

Inheritance & virtual functions Vs Generic Programming

老子叫甜甜 提交于 2019-12-03 23:45:13
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 Design Principles I always like to think of templates and inheritance as two orthogonal concepts, in the very literal sense: To me, inheritance goes "vertically", starting with a base class at the top and going "down" to more and more derived classes. Every (publically) derived class is a base class in terms of its interface: A poodle is a dog is an

How do I determine the number of parameters of a std::function?

冷暖自知 提交于 2019-12-03 20:29:26
I have the following problem. Say you want to write a generic function that can take a lambda expression. I understand that if the parameter is of type std::function, then I could not only use lambdas, but also functions and even pointers to functions. So at a first step, I did the following: void print(std::function<void(int, int)> fn) { fn(1,2); } int main() { print([](int i, int j) { std::cout << j <<','<<i<<'\n'; }); return 0; } Now the problem is that I want to make this function generic, meaning that I don't want the lambda expression to have only two parameters. So I tried changing the

How to make controller endpoint to get two different objects in java spring?

别等时光非礼了梦想. 提交于 2019-12-03 20:25:23
I have a server built with java and spring. What i am trying to do is that my controller with the same endpoint will get two different objects. This is an example for what I mean: I know I can do that: public class Option1{ private String name; ... //getter and setter } public class Option2{ private Long id; ... //getter and setter } @Controller public class Controller{ @RequestMapping(value = "service/getData/option1", method = RequestMethod.POST) @ResponseBody public String searchProv(@ResponseBody Option1 data1){ return "option1" } @RequestMapping(value = "service/getData/option2", method =

How do I build gcc with C++ concepts (“concepts lite”) support?

半世苍凉 提交于 2019-12-03 19:46:14
问题 The C++ standards committee is working on a TS (Technical Specification) for Concepts extension: "Programming Languages - C++ Extensions for Concepts". N4377 is the latest version of this document. For inclusion into the C++ standard features are asked to be implemented, ideally for a publicly accessible system. I'm aware of concept-gcc but the concepts proposal above (colloquially referred to as Concepts Lite ) is different. I heard that there is a concepts branch and I have tried the origin

Constructor for nested initializer lists

为君一笑 提交于 2019-12-03 16:42:46
Is it possible to have a generic constructor that takes any type of initializer list, even if this has nested lists within? Say you have the following partial template specialization for a class that takes in its constructor nested initializer lists: template class ClassA; template <> class ClassA<4> { typedef std::initializer_list<double> list_type; typedef std::initializer_list<list_type> llist_type; typedef std::initializer_list<llist_type> lllist_type; typedef std::initializer_list<lllist_type> initializer_type; size_t n_[4] = {0}; double* data_; public: ClassA(initializer_type l) { assert

Indexing into containers: the mathematical underpinnings

元气小坏坏 提交于 2019-12-03 12:02:43
问题 When you want to pull an element out of a data structure, you have to give its index. But the meaning of index depends on the data structure itself. class Indexed f where type Ix f (!) :: f a -> Ix f -> Maybe a -- indices can be out of bounds For example... Elements in a list have numeric positions. data Nat = Z | S Nat instance Indexed [] where type Ix [] = Nat [] ! _ = Nothing (x:_) ! Z = Just x (_:xs) ! (S n) = xs ! n Elements in a binary tree are identified by a sequence of directions.

When/Why ( if ever ) should i think about doing Generic Programming/Meta Programming

一曲冷凌霜 提交于 2019-12-03 04:23:19
问题 IMHO to me OOPS, design patterns make sense and i have been able to apply them practically. But when it comes to "generic programming /meta programming" of the Modern C++ kind, i am left confused. -- Is it a new programming/design paradigm ? -- Is it just limited to "library development"? If not, What design/coding situations call for using meta programming/generic programming. -- Does using templates mean i am doing generic programming? I have googled a lot on this topic but do not grasp the

Indexing into containers: the mathematical underpinnings

不羁的心 提交于 2019-12-03 01:29:15
When you want to pull an element out of a data structure, you have to give its index. But the meaning of index depends on the data structure itself. class Indexed f where type Ix f (!) :: f a -> Ix f -> Maybe a -- indices can be out of bounds For example... Elements in a list have numeric positions. data Nat = Z | S Nat instance Indexed [] where type Ix [] = Nat [] ! _ = Nothing (x:_) ! Z = Just x (_:xs) ! (S n) = xs ! n Elements in a binary tree are identified by a sequence of directions. data Tree a = Leaf | Node (Tree a) a (Tree a) data TreeIx = Stop | GoL TreeIx | GoR TreeIx --

Generics/templates in python?

末鹿安然 提交于 2019-12-03 00:51:38
问题 How does python handle generic/template type scenarios? Say I want to create an external file "BinaryTree.py" and have it handle binary trees, but for any data type. So I could pass it the type of a custom object and have a binary tree of that object. How is this done in python? 回答1: Python uses duck typing, so it doesn't need special syntax to handle multiple types. If you're from a C++ background, you'll remember that, as long as the operations used in the template function/class are

Why do we need containers?

独自空忆成欢 提交于 2019-12-02 19:04:18
(As an excuse: the title mimics the title of Why do we need monads? ) There are containers (and indexed ones) (and hasochistic ones) and descriptions . But containers are problematic and to my very small experience it's harder to think in terms of containers than in terms of descriptions. The type of non-indexed containers is isomorphic to Σ — that's quite too unspecific. The shapes-and-positions description helps, but in ⟦_⟧ᶜ : ∀ {α β γ} -> Container α β -> Set γ -> Set (α ⊔ β ⊔ γ) ⟦ Sh ◃ Pos ⟧ᶜ A = ∃ λ sh -> Pos sh -> A Kᶜ : ∀ {α β} -> Set α -> Container α β Kᶜ A = A ◃ const (Lift ⊥) we are