generic-programming

Partial specialization: use the primary template members

让人想犯罪 __ 提交于 2019-12-06 13:44:29
问题 Consider enum My_Enum { x1, x2 }; template<class T, My_Enum X> class A { void f1(); void f2(); }; template<class T> class A<T,x1> { void g(); } I want to use the member functions f1() and f2() of the primary template in my partially specialized template. What should I do ? One solution would be not to do the partial specialization and then: template<class T> class AA<T> : public A<T,x1> { void g(); } but it has the drawback that when I'm instatiating A<T,X> s of all sorts by generic

Scala: “Static values” in traits?

浪尽此生 提交于 2019-12-06 11:04:06
Let's say I have: trait X { val x: String } Using mix-in, I can define a trait such as trait XPrinter { self: X => def printX: String = "X is: " + x } such that a value/object implementing XPrinter implements x and give its methods such as printX access to the values specified in X such as x . So far, so good. I want to know if there is a way of having a trait in the form of: trait XDependent[T <: X] { def printX: String = ??? } So that XDependent instances have access to the value of T.x , with x assumed to be a "static value" glued with the type definition. Now I understand why T.x can't be

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

China☆狼群 提交于 2019-12-06 10:52:15
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) { std::cout << "Specialized extractor called" << std::endl; } }; template <typename T> void extract

Trying to make templates in C

瘦欲@ 提交于 2019-12-06 09:44:57
I made a generic vector in C using macros. Is the concept viable or do I get a one-way trip to the bonfire for even thinking about it? #ifndef VECTOR_H #define VECTOR_H #define vector_at(vector, pos) ((vector).data[pos]) #define VECTOR_DEFINITION(type)\ typedef struct {\ size_t size;\ size_t capacity;\ type *data;\ } vector_ ## type ## _t;\ void vector_ ## type ## _reserve(vector_ ## type ## _t *vector, size_t size) {\ size_t capacity = 1;\ while (capacity < size) capacity *= 2;\ if (size == 0) capacity = 0;\ if (capacity != vector->capacity)\ {\ vector->capacity = capacity;\ if (size == 0) {\

generic class, how to set the type in runtime?

元气小坏坏 提交于 2019-12-06 08:54:17
问题 I have created a generic class, but I know the type in runtime, not in design, so I would like to know how to set the type in runtime. For example, I have: public class MyGenericClass<T> { .... } Then I try to use it. I have a method in other class, that consume this generic class. In the constructor of this class, I receive as parameter the type that I want, so I have a type property in which I save the type that I need. So I am trying this: MyGenericClass<_typeNeeded> myClass = new

How to get the Generic Type Parameter?

此生再无相见时 提交于 2019-12-06 07:18:17
问题 Simply: public static class MyClass<T> { // i don't want to keep an instance of T, if it is not necessary. // and it is not nice, not neat. // Or, let's say, the member are in the form of : ArrayList<T> mArrayList = new ArrayList<T>(); // the problem of getting the generic type parameter is still present. } @Test public final void test() { MyClass<Integer> myObject = new MyClass<Integer>(); getParamType( myObject ); } private static final <T> void getParamType(final MyClass<T> _myObject) {

How to extend from AsyncTask as a generic base class?

瘦欲@ 提交于 2019-12-06 04:14:52
问题 There are three different implementations of an AsyncTask : public class DownloadTask extends AsyncTask<String, Integer, Boolean> public class JsonParserTask extends AsyncTask<Object, Void, Boolean> public class PostCommentTask extends AsyncTask<String, Void, HttpRequestResult> I would like them to extend a BaseAsyncTask which I can use for dependency injection then. The class signatur of AsyncTask looks like this: public abstract class AsyncTask<Params, Progress, Result> How can I extend

Compiler cannot find right implicits for shapeless LabelledGeneric

蓝咒 提交于 2019-12-05 22:14:51
Continuing the discussion about converting case classes and Map[String,Any] , I faced some problems when I wanted to use it in a generic way, Here is the main discussion. And when I want to use a generic method like following, compiler complains about the implicits: import MapConvertor._ def convert[A](cc: A)= { cc.toMapRec } Here is the whole code that provides toMapRec : import shapeless._, labelled.FieldType, record._ trait ToMapRec[L <: HList] { def apply(l: L): Map[String, Any] } trait LowPriorityToMapRec { implicit def hconsToMapRec1[K <: Symbol, V, T <: HList](implicit wit: Witness.Aux

minmax that returns modifiable references

六眼飞鱼酱① 提交于 2019-12-05 22:11:29
With all the new features of C++ (C++11 suffices I think), what prevents from having a std::minmax function that returns a pair of references. In this way, if one feeds two modifable references, they can be modified. Is this opening a can of worms? #include<functional> // maybe all this options can be simplified template<class T1, class T2> struct common; template<class T> struct common<T, T>{using type = T;}; template<class T> struct common<T const&, T&>{using type = T const&;}; template<class T> struct common<T&, T const&>{using type = T const&;}; template<class T> struct common<T, T&>{using

How can I programatically produce this datatype from the other?

痞子三分冷 提交于 2019-12-05 20:50:50
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 programming techniques? Here's another example to be clear about the type of mapping I want to produce.