FAQ Database Discussion Community


How to avoid repetition of the enclosing class type when auto and decltype() cannot be used

c++,generic-programming
I recently discovered the auto and decltype() features of C++11, which are excellent, as they allow eliminating a lot of redundant type code. However, there are contexts where they cannot be used. One example I'm primarily asking about is if you want to declare a variable whose type uses the...

C++11: shortest way to explicitly copy a value to use as an rvalue reference

c++,c++11,copy-constructor,move-semantics,generic-programming
I have a function class A { <has copy and move constructors > }; void f(A &&a) { ... } I want to call the function f with a variable x, but I want to run f on a copy of x, not x itself. Why can't I do something...

C++ - Templates and Data Types

c++,templates,generic-programming
I am very new to C++ and am trying to create a "Generic Class" that can take any input. I need a way to store whatever input my class receives in either an Array or a Vector. I am however struggling to figure out how to do that. I tried...

C Pass arguments as void-pointer-list to imported function

c++,c,pointers,function-pointers,generic-programming
I want to import an external function from a DLL via the LoadLibrary function and wanted to ask if I could pass a void-pointer-list to the function-pointer which I got returned by the LoadLibrary function. The problem I have is that I want to use a generic application that can...

Fortran generic procedure error: may not be generic

fortran,procedure,generic-programming,fortran2003
I know that you can make generic procedures using abstract types, like in here: fortran class declaration of dummy argument But can I do the same sort of thing with the following code? module proc_mod public :: forced,ideal interface forced; module procedure forced1; end interface interface forced; module procedure forced2;...

Java Generic Advanced Usage

java,generics,java-8,generic-programming
The problem is that the following code can't compile if the generic signature consists of several ? which are the same type. import java.util.Map; import java.util.HashMap; import java.util.function.Function; public class Test { private static <T> T findSelfReference(Map<T, T> map) { for (Map.Entry<T, T> entry : map.entrySet()) { if (entry.getKey() ==...

Generic Programming in Go. Avoiding hard coded type assertion

reflection,go,generic-programming
I'm programming a generic cache mechanism and i need to set some attributes in a struct knowing only their reflect.Type, attribute name and reflect.Value to be setted in the attribute, but i can't avoid the type assertion, that makes my code not generic... func main() { addressNew := Address{"New Address...

Why does the signature of deriveHCons declare `HK <: Symbol` when Symbol is a final class

scala,generics,subclassing,generic-programming,shapeless
Here is the signature of deriveHCons in LabelledProductTypeClassCompanion of Shapeless: implicit def deriveHCons[HK <: Symbol, HV, TKV <: HList] (implicit ch: Lazy[C[HV]], key: Witness.Aux[HK], ct: Lazy[Wrap[TKV] { type V <: HList }] ): Wrap.Aux[FieldType[HK, HV] :: TKV, HV :: ct.value.V] = ... It seems strange to me that we declare...

Why does not work the use of an extension method in the same extension method?

vb.net,reflection,extension-methods,dynamic-programming,generic-programming
I got an extension method that gives me the value of every property in an instance. For scalar values works it fine. But for Collections there is a problem. This is my code: <Extension()> Public Function ToXml(Of T)(ByVal source As T) As XmlDocument Dim oXmlDocument As New XmlDocument oXmlDocument.AppendChild(oXmlDocument.CreateXmlDeclaration("1.0", "utf-8",...

Using Generic types to calculate with floats and ints dynamically

java,generics,generic-programming
Following code is a part of a project in which I need to determine the minimum/maximum of datacolumns. The problem is, I don't know whether the column will contain floats or ints. My attempt at making a generic class to do this is the following but this gives me the...

No instance for (Generic (f a)) arising from a use of `from'

haskell,generic-programming,ghc-generics
I'm having issues with finding suitable type constraints for the following code {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE FlexibleContexts #-} import GHC.Generics data Value = One | Two deriving Generic class Class a where cname :: a -> String -> Bool default...

What is the difference between these class declarations with Comparable?

java,generics,tree,comparable,generic-programming
Here is one declaration public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> { .... } Here is the other declaration public class BinarySearchTree<AnyType extends Comparable<AnyType>>{ ..... } I know the first declaration is preferred(seen it in alot of java examples and textbooks) but why is that? All my code ran fine...

Confused about returns in stack template

c++,templates,generic-programming
I'm implementing a generic stack (with an array) in C++ and am confused about what to return in this situation: template <class T> T Stack<T>::pop(void) { if (size != 0) { return items[size - 1]; size--; } else { cerr << "Cannot pop from empty stack." << endl; return ???;...

_Generic: multiple types to a single value?

generic-programming,c11
Using c11 _Generic is there a way to map multiple types to a single value? eg: _Generic(e, \ A *: foo_expression, \ B **: foo_expression, \ C: foo_expression, \ enum D: bar_expression, \ void *: bar_expression) Is there a way to group types? (this isn't valid syntax just to express...

Go reflect.MakeFunc. How to return a err=nil as reflect.Value?

reflection,go,generic-programming
How to return a err=nil as reflect.Value? I need to write a swap function to use with reflect.MakeFunc(). //my swap implementation, that call the original function and cache results func swapFunc(ins []reflect.Value) []reflect.Value { //After cache the first return (Offer) of function FindBestOffer(int)(Offer,bool,error), //i need to return the best Offer...

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

c++,g++,generic-programming,c++-concepts,c++1z
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...

Dynamically generate object field of generic type

c#,dynamic,generic-programming
Code: objectType request = factory.create<objectType>(); public class factory { public static T create<T>() where T : new() { T obj = new T(); PropertyInfo propertyInfo = obj.GetType().GetProperty("client_no"); propertyInfo.SetValue(obj, CLIENT_NUMBER, null); return (T)Convert.ChangeType(obj, typeof(T)); } } Explanation: I am creating a generic factory fn() that sets 2 object properties. These properties...

Why would a template class have an unused type?

c++,generic-programming,template-classes,boost-units
I'm reviewing the boost units library and I am puzzled why the boost::units::unit class has an extra template parameter. Here is the example: http://www.boost.org/doc/libs/1_57_0/boost/units/unit.hpp template<class Dim,class System, class Enable> class unit { public: typedef unit<Dim, System> unit_type; typedef unit<Dim,System> this_type; typedef Dim dimension_type; typedef System system_type; unit() { } unit(const...

What is “Scrap Your Boilerplate”?

haskell,functional-programming,generic-programming,scrap-your-boilerplate
I see people talking about Scrap Your Boilerplate and generic programming in Haskell. What do these terms mean? When would I want to use Scrap Your Boilerplate, and how do I use it?

How do I return a generic type while having another generic type as parameter, both required to implement an interace?

java,generics,interface,generic-programming,generic-method
Okay yes, the title is kind of confusing. But this is what I want to accomplish: I want to return a list containing elements of a type C. And I want the method to receive a variable of type R. And C have to be a class implementing an interface,...

How to do to have a generic Entity for SQL audit tables with JPA or Hibernate?

generics,jpa,entity,reverse-engineering,generic-programming
I want to show all datas of my audit tables in a jsf page from SQL server. I dont khnow how to do for this ? I have to khnow the fieled of every audit table in the database to create a Entity class , or there a generic Entity...