generic-programming

Partial specialization: use the primary template members

心不动则不痛 提交于 2019-12-04 19:15:16
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 programming, my A<T,x1> are no longer of type AA<T> and hence I cannot apply A<T,x1>.g() Any idea ? How about

nested-name-specifier

不问归期 提交于 2019-12-04 17:09:56
问题 I have a code like: namespace mymap { template <class Key,template <typename T > class Allocator> myownmap { typedef pair<const unsigned int, Key> typename _myPair; typedef multimap<unsigned int, Key,less<Key> ,Allocator<_myPair> > typename _entriesType; } } It compiles successfully (and works) under MSVC, but gcc is complaining about invalid syntax: .hpp:20: error: expected nested-name-specifier before ‘_myPair’ .hpp:20: error: two or more data types in declaration of ‘_myPair’ what i'm

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

感情迁移 提交于 2019-12-04 14:31:39
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 new T("foo"); } So, I tried the same technique with a "le" function: class Main { @:generic static

How to get the Generic Type Parameter?

我们两清 提交于 2019-12-04 14:11:49
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) { System.out.println(_myObject.getClass().getTypeParameters()[0]); // T System.out.println(((T) new Object(

How to extend from AsyncTask as a generic base class?

会有一股神秘感。 提交于 2019-12-04 10:18:31
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 AsyncTask while maintaining the different parameters? | DownloadTask AsyncTask <-- BaseAsyncTask <--|

Datatype-generic programming libraries for Scala

陌路散爱 提交于 2019-12-04 09:15:37
问题 I'm looking for a Scala library allowing for datatype-generic programming (like Scrap Your Boilerplate, for example). A list of libraries with appropriate links and short descriptions for each one would be a perfect answer. 回答1: Well, Adrian Moors has reimplemented Jeremy Gibbons' Origami programming : The paper. The source. Bruno Oliveira and Jeremy Gibbons have re-implemented Hinze's Generics for the masses , Lämmel & Peyton-Jones' Scrap your Boilerplate with Class , and Origami Programming

What is Haskell's Data.Typeable?

孤人 提交于 2019-12-04 07:29:51
问题 I've come across references to Haskell's Data.Typeable , but it's not clear to me why I would want to use it in my code. What problem does it solve, and how? 回答1: Data.Typeable is an encoding of an well known approach (see e.g. Harper) to implementing delayed (dynamic) type checking in a statically typed language -- using a universal type. Such a type wraps code for which type checking would not succeed until a later phase. Rather than reject the program as ill-typed, the compiler passes it

Grouping data types by constructor in Haskell

北城以北 提交于 2019-12-04 03:18:22
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]] 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 _) (Z _) = True valCtorEq _ _ = False And then: *Main Data.List> groupBy valCtorEq $ sort vals [[X 1,X 2],[Y

How to implement a generic macro in C?

筅森魡賤 提交于 2019-12-04 03:08:52
FUNC(param); When param is char * ,dispatch to func_string . when it's int ,dispatch to func_int I think there may be a solution to this,as variable types are known at compile time.. This will be possible with C1X but not in the current standard. It will look like this: #define cbrt(X) _Generic((X), long double: cbrtl, \ default: cbrt, \ float: cbrtf)(X) Variable types are known to the compiler, but not to the preprocessor (which sees the code simply as unstructured text a stream of tokens, and performs only simple replacement operations on it). So I am afraid you can't achieve this with C

Adding a custom view to XML… but with a GENERIC-type

雨燕双飞 提交于 2019-12-04 01:14:05
I am working on a custom view with a hope of reusability. It should have a generic type, like this: public class CustomViewFlipper<someType> extends ViewFlipper { } I know how to bind a normal custom view to the XML file. But I couldn't find any example for this situation. Is there any way to define a generic type for a class in XML? I don't think so, but you can create your own subclass: public class TheClassYouPutInTheLayoutFile extends CustomViewFlipper<someType> and use that class in your layout XML. As type parameters are actually cleared off in bytecode, you can use in XML the class name