For my current homework, I'm trying to sort my array through a generic class as the user inserts values into its locations. When the size reads as fully loaded, the array class calls in an expansion method that increases the size of the array while retaining its values in proper locations, which I followed from my Professor's note. For s...
I'd like to know the precise yet succinct definitions of these three concepts in one place. The quality of the answer should depend on the following two points.
Show a simple code snippet to show how and what the concept/technique is used for.
Be simple enough to understand so that a programmer without any exposure to this area can gra...
How does one go about implementing a subtype of Numeric[T]?
I have been looking for at guide on this but haven't found any.
Example of subtypes could be Rational or Complex?
Thanks in advance
Troels
...
I have a tree_node class and a tree class.
template<typename T>
class tree_node
{
public:
tree_node(const std::string& key_, const T& value_)
: key(key_), value(value_)
{
}
private:
T value;
std::string key;
};
template<typename T>
class tree
{
public:
tree() : root(new tree_node<T>("", ???)) { }
private:
...
I would like to create a construct similar to std::iterator_traits::value_type that can work seamlessly for all types using the same syntax. Imagine we have the following:
template <typename T>
struct value_type {
typedef T type;
};
#define VALUE_TYPE(T) typename value_type<T >::type
This will work for POD types. I can specialize...
I'm using generics rather long time but I've never used construction like List<? super T>.
What does it mean? How to use it? How does it look after erasure?
I also wonder: is it something standard in generic programming (template programming?) or it's just a java 'invention'? Does c#, for example, allow similar constructions?
...
I am pretty new to Haskell (still working on totally understanding monads). I have a problem where I have a tree like structure
type Tree = [DataA]
data DataA = DataA1 [DataB]
| DataA2 String
| DataA3 String [DataA]
deriving Show
data DataB = DataB1 [DataA]
| DataB2 String
...
Basically i just want to do an arbitrary operation using given arguments of arbitrary types.
Argument type base class is Var, and Operation is base class of the operation that will executed for given arguments.
I have Evaluator class, that hold a collection of operators which mapped using opId. Evaluator will do operation based on opId...
How do I convert the TypeIdenitifier to a class type? I need to use implicit convertion.
type
TMyChildArray<T>=class(TMyArray<T>)
private
FData:Array of T;
procedure AddEnd();
end;
TTypeIdenitifierParentClass=class(TAnotherParentClass)
protected
TestField:Cardinal;
end;
procedure TMyChildArray<T>.A...
The questions are in bold, for those that cannot be bothered reading a question in depth.
This is a followup to this question. It is to do with the initialization semantics of static variables in functions. Static variables should be initialized once, and their internal state might be altered later - as I (currently) do in the linked qu...
Hi,
In Scheme, procedures like +, -, *, / works on different types of numbers, but we don't much see any other generic procedures.
For example, length works only on list so that vector-length and string-length are needed.
I guess it comes from the fact that the language doesn't really offer any mechanism for defining generic procedure...
I'm creating a generic list class that has a member of type Array(Array of ).
The problem is the class destruction,because the class is supposed to be used for types from byte to types inheriting TObject.
Specifically:
destructor Destroy;
var elem:T;
begin
/*if(T is Tobject) then //Check if T inherits TObject {Compiler error!}
f...
This is what I would like to do using templates:
struct op1
{
virtual void Method1() = 0;
}
...
struct opN
{
virtual void MethodN() = 0;
}
struct test : op1, op2, op3, op4
{
virtual void Method1(){/*do work1*/};
virtual void Method2(){/*do work2*/};
virtual void Method3(){/*do work3*/};
virtual void Method4(){/*...
Some of the disadvantages would be
its syntax is complex
compiler generates extra code
...
I get "E2511 Type parameter 'T' must be a class type" on the third class.
type TSomeClass=class
end;
ParentParentClass<T>=class
end;
ParentClass<T: class> = class(ParentParentClass<T>)
end;
ChildClass<T: TSomeClass> = class(ParentClass<T>)
end;
I'm trying to write a lite Generic Array wrapper for any data type(ParentParentClass) ,...
type
TParent=class
public
member1:Integer;
end;
TChild=class(TParent)
public
member2:Integer;
end;
TGArray<T: TParent>=class
public
function test:T;
end;
implementation
var g:TGArray<TChild>;
function TGArray<T>.test:T;
begin
Result:=??.create; // <<<< Problem !
end;
...
I have an arbitrary STL container C, which contains elements of an arbitrary type T. I want to create an std::vector that has a copy of all the elements. What is the cleanest way to do this?
template <typename C>
void myfunction(C container){
/*Derive the type T of elements within the container*/
std::vector<T> mystack;
...
I am writing matrix classes. Take a look at this definition:
template <typename T, unsigned int dimension_x, unsigned int dimension_y>
class generic_matrix
{
...
generic_matrix<T, dimension_x - 1, dimension_y - 1>
minor(unsigned int x, unsigned int y) const
{ ... }
...
}
template <typename T, unsigned int dimension>
class ...
In my application I have lots of different data types, e.g. Car, Bicycle, Person, ... (they're actually other data types, but this is just for the example).
Since I also have quite some 'generic' code in my application, and the application was originally written in C, pointers to Car, Bicycle, Person, ... are often passed as void-pointe...
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 followin...