generic-programming

How to declare a typedef that references itself?

梦想与她 提交于 2019-12-23 04:34:26
问题 I have a Variant type which I want to use in something like a JSON parser. A type in JSON can include objects and arrays. These objects and arrays can contain members of their own type: typedef variant<int,float,bool,std::string> baseType; typedef std::vector<baseType> arrayType; // problem, can't have arrays of arrays typedef std::unordered_map<std::string,baseType> objType; // problem, can't have objects or arrays within objects. How do I create a "recursive" templated type? Something like:

Function Overloading Based on Arbitrary Properties of Types doesn't work

。_饼干妹妹 提交于 2019-12-22 12:39:32
问题 In the example below, I need to extract some values. I have an efficient extractor, which can work with builtin types, and an inefficient template that can work with everything. To choose between these, I want to use Function Overloading Based on Arbitrary Properties of Types. Here is my code: #include <string> #include <iostream> class extractor { public: static void extract(const bool& val) { std::cout << "Specialized extractor called" << std::endl; } static void extract(const double& val)

How can I programatically produce this datatype from the other?

谁说胖子不能爱 提交于 2019-12-22 09:48:29
问题 I'd like to use DSum for something. To work with DSum , you need to have a 'tag' type which takes one type argument, e.g. data Tag a where AFirst :: Tag Int ASecond :: Tag String However, I'd like to use this internally in a library. I want the interface that I expose to users to take any old datatype, e.g. data SomeUserType1 = Foo Int | Bar String it's clearly quite mechanical to go from this to the Tag a type given above. So, is it possible to do this in code, with some sort of generic

Refactoring a “dumb” function into generic STL-style with iterators to containers

孤人 提交于 2019-12-22 08:06:55
问题 I've managed to wrap my head around some of C++'s functional capacities (for_each, mapping functions, using iterators...) but the construction of the templates and function argument lists for taking in generic containers and iterators still eludes me. I have a practical example I'm hoping someone can illustrate for me: Take the following function that processes an incoming std::vector and builds a running total of many data-points/iterations of a process: /* the for-loop method - not very

Using Typeable to partially apply function at run-time (any time types match)

泄露秘密 提交于 2019-12-22 05:43:19
问题 Generic programming time! If I have a function: f :: a1 -> a2 -> a3 -> ... -> an and a value v :: aX -- where 1 <= x < n Without knowing at compile time which of the arguments of f the value v is the right type for (if any), can I partially apply f to v ? (using Typeable, Data, TH, or any other trick) Slightly more solidly, can I construct the function g (below) at run-time? It doesn't actually have to be polymorphic, all my types will be monomorphic! g :: (a1 -> a2 -> a3 -> a4 -> a5) -> a3 -

how to write a generic compare function in Haxe (haxe3)

北慕城南 提交于 2019-12-21 20:30:51
问题 I am trying to write a generic compare function (like the c strcmp) in Haxe3 for a template type A, assuming that this template type has a less-than-or-equal-to operator "<=". I saw in the Haxe3 documentation (http://haxe.org/manual/haxe3/features) that you can do a similar job if you want to assume that a template type has the new function: @:generic static function foo<T:{function new(s:String):Void;}>(t:T) { trace(Type.typeof(t)); // TClass([class String]) / TClass([class Template]) return

Grouping data types by constructor in Haskell

社会主义新天地 提交于 2019-12-21 09:35:32
问题 Given this data type data Val = X Int | Y Bool | Z Double deriving (Eq, Show) and a list such as let vals = [X 1, Z 2.7, Y True, X 2, Z 3.14, Y True] how to group elements in vals into this list, [[X 1,X 2],[Y True,Y True],[Z 2.7, Z 3.14]] 回答1: I've the following: data Val = X Int | Y Bool | Z Double deriving (Eq, Ord, Show) vals :: [Val] vals = [X 1, Z 2.7, Y True, X 2, Z 3.14, Y True] valCtorEq :: Val -> Val -> Bool valCtorEq (X _) (X _) = True valCtorEq (Y _) (Y _) = True valCtorEq (Z _)

Inheritance & virtual functions Vs Generic Programming

只愿长相守 提交于 2019-12-21 07:31:34
问题 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 回答1: 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

Why do we need containers?

拜拜、爱过 提交于 2019-12-20 09:23:15
问题 (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

What does “typename =” mean in the template parameters?

前提是你 提交于 2019-12-20 09:11:39
问题 I have seen this expression in page 189 of the book "Effective Modern C++": template<typename T, typename = typename std::enable_if<condition>::type> explicit Person(T&& n); I am just wondering what does the part " typename = " mean. It certainly looks like a default argument for a template parameter. But don't you need something like " typename some_name = ... " in a default argument? There is no name for the second template argument, and I don't see the second template argument being used