CSC/ECE 517 Fall 2012/ch1b 1w47 sk: Difference between revisions
Line 290: | Line 290: | ||
Generics generate different classes based on the input type. An std::vector<int> is a completely different class than an std::vector<float>. However, since C++ templates create different classes for every variation of their template parameters, the size of the compiled executable is larger. Beyond that, compilation time increases greatly, since all template code must be included with each compilation unit and much more code must be generated. However, actual runtime memory footprint is typically smaller than the alternative (frees an extra void*) and performance is better, since the compiler can perform more aggressive optimizations with the known type. While a generic Java class compiles it's entire self, when using a C++ template, you only compile what you use. So, if you create an std::vector<int> and only use push_back and size, only those functions will be compiled into the object file. This eases the size of executable problem. | Generics generate different classes based on the input type. An std::vector<int> is a completely different class than an std::vector<float>. However, since C++ templates create different classes for every variation of their template parameters, the size of the compiled executable is larger. Beyond that, compilation time increases greatly, since all template code must be included with each compilation unit and much more code must be generated. However, actual runtime memory footprint is typically smaller than the alternative (frees an extra void*) and performance is better, since the compiler can perform more aggressive optimizations with the known type. While a generic Java class compiles it's entire self, when using a C++ template, you only compile what you use. So, if you create an std::vector<int> and only use push_back and size, only those functions will be compiled into the object file. This eases the size of executable problem. | ||
<br> | |||
<br> | |||
=== Generics in C++=== | === Generics in C++=== |
Revision as of 02:13, 2 October 2012
Introduction
Generic Programming is a programming paradigm for developing efficient and reusable software libraries. The Generic Programming process focuses on finding commonality among similar implementations of the same algorithm, then providing suitable abstractions so that a single, generic algorithm can cover many concrete implementations. This process is repeated until the generic algorithm has reached a suitable level of abstraction, where it provides maximal re-usability while still yielding efficient, concrete implementations.
In the simplest definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by Ada in 1983, permits writing common functions or types that differ only in the set of types on which they operate on, thus reducing duplication. Such software entities are known as generics in Ada, Eiffel, Java and .NET; parametric polymorphism in ML, Scala and Haskell ; templates in C++; and parameterized types in the influential 1994 book, Design Patterns.
The term generic programming was originally coined by David Musser and Alexander Stepanov in a more specific sense than the above, to describe an approach to software decomposition whereby fundamental requirements on types are abstracted from across concrete examples of algorithms and data structures and formalized as concepts, analogously to the abstraction of algebraic theories in abstract algebra. Early examples of this programming approach were implemented in Scheme and Ada, although the best known example is the Standard Template Library (STL) in which is developed a theory of iterators which is used to decouple sequence data structures and the algorithms operating on them.
Generics in Object Oriented Languages
Generics .NET framework
Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use. A generic collection class might use a type parameter as a placeholder for the type of objects that it stores; the type parameters appear as the types of its fields and the parameter types of its methods. A generic method might use its type parameter as the type of its return value or as the type of one of its formal parameters.
The following code illustrates a simple generic class definition in C#.
public class Generic<T> { public T Field; }
The following code illustrates a simple generic class definition in Visual Basic.
Public Class Generic(Of T) Public Field As T End Class
When an instance of a generic class is created, one can specify the actual types to substitute for the type parameters. This establishes a new generic class, referred to as a constructed generic class, with the chosen types substituted everywhere that the type parameters appear. The result is a type-safe class that is tailored to the user's choice of types, which is illustrated in the code below in C# and VB.
//Code in C# public static void Main() { Generic<string> g = new Generic<string>(); g.Field = "A string"; }
'Code in Visual Basic Public Shared Sub Main() Dim g As New Generic(Of String) g.Field = "A string" End Sub
Generics Terminology in .NET
- A generic type definition is a class, structure, or interface declaration that functions as a template, with placeholders for the types that it can contain or use. For example, the Dictionary class can contain two types: keys and values. Because a generic type definition is only a template, so one cannot create instances of a class, structure, or interface that is a generic type definition.
- Generic type parameters, or type parameters, are the placeholders in a generic type or method definition. The Dictionary generic type has two type parameters, TKey and TValue, that represent the types of its keys and values.
- A constructed generic type, or constructed type, is the result of specifying types for the generic type parameters of a generic type definition.
- A generic type argument is any type that is substituted for a generic type parameter.
- The general term generic type includes both constructed types and generic type definitions.
- Covariance and contravariance of generic type parameters enable one to use constructed generic types whose type arguments are more derived (covariance) or less derived (contravariance) than a target constructed type. Covariance and contravariance are collectively referred to as variance.
- Constraints are limits placed on generic type parameters. For example, one might limit a type parameter to types that implement the IComparer generic interface, to ensure that instances of the type can be ordered. One can also constrain type parameters to types that have a particular base class, that have a default constructor, or that are reference types or value types. Users of the generic type cannot substitute type arguments that do not satisfy the constraints.
- A generic method definition is a method with two parameter lists: a list of generic type parameters and a list of formal parameters. Type parameters can appear as the return type or as the types of the formal parameters. The following code shows this in C# and Visual Basic.
//Code in C# T Generic<T>(T arg) { T temp = arg; //... return temp; }
'Code in Visual Basic Function Generic(Of T)(ByVal arg As T) As T Dim temp As T = arg '... Return temp End Function
- Generic methods can appear on generic or nongeneric types. A method is not generic just because it belongs to a generic type, or even because it has formal parameters whose types are the generic parameters of the enclosing type. A method is generic only if it has its own list of type parameters. In the following code, only method G is generic.
//Code in C# class A { T G<T>(T arg) { T temp = arg; //... return temp; } } class Generic<T> { T M(T arg) { T temp = arg; //... return temp; } }
'Code in Visual Basic Class A Function G(Of T)(ByVal arg As T) As T Dim temp As T = arg '... Return temp End Function End Class Class Generic(Of T) Function M(ByVal arg As T) As T Dim temp As T = arg '... Return temp End Function End Class
Nested Types and Generics
A type that is nested in a generic type can depend on the type parameters of the enclosing generic type. The common language runtime considers nested types to be generic, even if they do not have generic type parameters of their own. When you create an instance of a nested type, you must specify type arguments for all enclosing generic types.
Language Support
The .NET Framework provides a number of generic collection classes in the following namespaces:
- The System.Collections.Generic namespace catalogs most of the generic collection types provided by the .NET Framework, such as the List and Dictionary generic classes.
- The System.Collections.ObjectModel namespace catalogs additional generic collection types, such as the ReadOnlyCollection generic class, that are useful for exposing object models to users of your classes.
- Generic interfaces for implementing sort and equality comparisons are provided in the System namespace, along with generic delegate types for event handlers, conversions, and search predicates.
- The common language runtime provides new opcodes and prefixes to support generic types in Microsoft intermediate language (MSIL), including Stelem, Ldelem, Unbox_Any, Constrained, and Readonly.
- Visual C++, C#, and Visual Basic all provide full support for defining and using generics.
Generics in Java
Generics were introduced in Java from JDK 1.5. Generics allows the abstraction over types. The most common examples are container types, such as those in the Collection hierarchy. So, if one could say that the code works with some unspecified type, rather than a specific interface or class, then the code uses Generics. Below is an example of a Genric:
List myIntList = new LinkedList(); myIntList.add(new Integer(0)); Integer x = (Integer) myIntList.iterator().next();
Typically, a programmer knows what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required. Of course, the cast not only introduces clutter, but also introduces the possibility of a run time error, since the programmer might be mistaken. It would be better if programmers could actually express their intent, and mark a list as being restricted to contain a particular data type. This is the core idea behind generics.
Here is a variation of the program fragment given above using generics:
List<Integer> myIntList = new LinkedList<Integer>(); myIntList.add(new Integer(0)); Integer x = myIntList.iterator().next();
The type declaration for the variable myIntList specifies that this is not just an arbitrary List, but a List of Integer, written List<Integer>. The List is a generic interface that takes a type parameter - in this case, Integer. One has to specify a type parameter when creating the list object. The compiler can now check the type correctness of the program at compile-time. Because myIntList is declared with type List<Integer>, this indicates that myIntList holds a list of integers, which holds true wherever and whenever it is used, and the compiler ensures to guarantee it. In contrast, the cast tells something the programmer thinks is true at a single point in the code. The net effect, especially in large programs, is improved readability and robustness.
Below is code for generic interfaces List and Iterator in package java.util.The content in angle brackets are the declarations of the formal type parameters of the interfaces List and Iterator.
public interface List<E> { void add(E x); Iterator<E> iterator(); } public interface Iterator<E> { E next(); boolean hasNext(); }
Generics and Subtyping
Below is an example showing code for Generics and Subtyping.
List<String> ls = new ArrayList<String>(); //1 List<Object> lo = ls; //2 lo.add(new Object()); //3 String s = ls.get(0); //4 attempts to assign an Object to a String
Line 1 is legal. Lines 2,3 and 4 aliased ls and lo. Accessing ls, a list of String, through the alias lo, one can insert arbitrary objects into it. As a result ls does not hold just Strings anymore. The Java compiler will prevent this from happening and Line 2 causes a compile time error.In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>.
Wildcards
Consider the problem of writing a routine that prints out all the elements in a collection. In older version's of Java, it can be written as below:
void printCollection(Collection c) { Iterator i = c.iterator(); for (k = 0; k < c.size(); k++) { System.out.println(i.next()); } }
In the later versions of Java, the same logic above can be written using generics using the syntax of enhanced for loop:
void printCollection(Collection<Object> c) { for (Object e : c) { System.out.println(e); } }
The old code could be called with any kind of collection as a parameter, the new code only takes Collection<Object>, which is not a supertype of all kinds of collections. The supertype of all kinds of collections is written Collection<?>, that is, a collection whose element type matches anything. It’s called a wildcard type. Below is the code expressing the same functionality using a wildcard.
void printCollection(Collection<?> c) { for (Object e : c) { System.out.println(e); } }
The above code can be called with any type of collection. Inside printCollection(), one can still read elements from c and give them type Object. This is always safe, since whatever the actual type of the collection, it does contain objects. However, It is not safe to add arbitrary objects to it.
Generic Methods
Generic methods are used more often than generic classes. A generic class is independent of whether you have a generic method. If a method is static, it has no access to the generic type parameters of the class, so if it needs to use genericity it must be a generic method. Unlike generic class one don’t usually have to specify the parameter types, because the compiler can figure that out.
public class Gen { public <T> void f(T x) { System.out.println(x.getClass().getName()); } public static void main(String[] args) { Gen a = new Gen(); a.f(""); a.f(‘c’); } }
Erasure
To implement generics, the Java compiler applies type erasure to:
- Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
- Insert type casts if necessary to preserve type safety.
- Generate bridge methods to preserve polymorphism in extended generic types.
Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.
Consider the example below.
public class A { public static void main(String[] args) { System.out.println(new ArrayList<String>().getClass() == new ArrayList<Integer>().getClass()); } }
Here ArrayList<String> and ArrayList<Integer> seem to be of different types and so one expects them to behave differently. But the execution of the above program returns true which suggests they are of the same type.
class Fn {} class Foo<Q> {} class Boo<X,Y> {} public class AbsentInformation { public static void main(String[] args) { Foo<Fn> foo = new Foo<Fn>(); Boo<Long,Double> b = new Boo<Long,Double>(); System.out.println(Arrays.toString(foo.getClass().getTypeParameters())); System.out.println(Arrays.toString(b.getClass().getTypeParameters())); } } Output: [Q] [X,Y]
Class.getTypeParameters() suggest that one might be able to find out what the parameter types are. But in reality, the identifiers that are used as the parameter placeholders. This is one important feature where Java differs from C++. Java generics are implemented using erasure.
So when one uses generics, the specific information is erased. The only thing one knows is that we are using an object. So both List<String> and List< Integer> have the same type at runtime.
Advantages and Disadvantages
Generics generate different classes based on the input type. An std::vector<int> is a completely different class than an std::vector<float>. However, since C++ templates create different classes for every variation of their template parameters, the size of the compiled executable is larger. Beyond that, compilation time increases greatly, since all template code must be included with each compilation unit and much more code must be generated. However, actual runtime memory footprint is typically smaller than the alternative (frees an extra void*) and performance is better, since the compiler can perform more aggressive optimizations with the known type. While a generic Java class compiles it's entire self, when using a C++ template, you only compile what you use. So, if you create an std::vector<int> and only use push_back and size, only those functions will be compiled into the object file. This eases the size of executable problem.
Generics in C++
C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code that behaves the same for data of any type.
Defining Templates
A Template Definition starts with the keyword template, followed by a list of Template Parameters. What follows is then either a class definition, or a function definition, defining a class template or a function template respectively. The template parameters introduce names into the scope of the definition, which can be types, values or templates. These names can be used just like any other name of the same kind. Then, when the template is instantiated, real types, values or templates are substituted in place of these names, and the code compiled.
Two template instantiations refer to the same template if their parameters are all the same, irrespective of any typedefs that may apply. Therefore vec1, vec2 and vec3 in the following example are all the same type.
typedef std::string MyString; typedef std::vector<std::string> T1; typedef std::vector<MyString> T2; T1 vec1; T2 vec2; std::vector<std::string> vec3;
Multiple parameters may be specified, separated by commas in the template parameter list:
template<typename T1,typename T2> class MyClass{};
MyClass is thus a class template with two template type parameters, T1 and T2. Every time a template is referenced with distinct template arguments, then the template is instantiated with the arguments. If the resultant code is not valid, then a compilation error will occur.
Templates can have one or more template parameters, which can be Type parameters, Non-Type parameters, or Template parameters.
Template Type Parameters
Template Type Parameters are template parameters that refer to a type; they are the most common form of template parameters. The syntax for Template Type Parameters is as below:
typename name or class name
Both alternatives are identical in meaning, and the choice is merely a matter of style. name is any valid C++ symbol name. Once name has been introduced in the template parameter list, then any reference to name within the body of the template automatically refers to the type of the corresponding template argument for each instantiation, and can be used anywhere a type can normally be used. e.g.
template<typename T> void func(T value) { const T& ref=value; T* p=new T; T temp(23); }
The code generated for func<int> is then identical to:
void func(int value) { const int& ref=value; int* p=new int; int temp(23); }
If, however, a reference was made to func<std::string>, then a compilation error would result, as the statement std::string temp(23); is not valid.
Template Non-Type Parameters
Non-Type Template Parameters are template parameters that are values rather than types. They can be any value that is a compile-time constant. Their syntax is akin to a variable declaration, e.g.:
template<int i> class A{}; template<double* dp> class B{}; template<void (*func)(int)> void c() {}
Class template A has a non-type template parameter which is an integer, class template B has a non-type template parameter which is a pointer-to-double, and function template c has a non-type template parameter which is a pointer to a function returning void, with a single int parameter.
Template Template Parameters
Template Template Parameters enable a template to be parameterized by the name of another template. Say, for example, there is a class which contains a couple of collections of items, some strings, some integers, and one wants the users of the class to choose what type of collection to use (vector, list, stack, etc.). The natural thought is to make the collection type a template parameter. However, a collection of strings is a different type from a collection of integers, so the user would have to specify both individually if template type parameters are used. The solution is a Template Template Parameter which is shown in the code below:
template<template<typename T> class ContainerType> class MyClass { ContainerType<int> intContainer; ContainerType<std::string> stringContainer; // rest of class };
ContainerType is a Template Template Parameter that refers to a template with a single Template Type Parameter. One can thus say MyClass<vector> or MyClass<list> to have vectors or lists respectively.Within the template definition, the Template Template Parameters can be used just like any other template.
Using Templates
Class templates and function templates can be used anywhere normal classes and functions can be, as well as as Template Template Parameters to other templates. However, the compiler needs to know what to use for the template parameters. For class templates, the template name must be followed by a template argument list, specifying the parameters. This is a comma-separated list of expressions between angle brackets (<>). For Template Type Parameters, the corresponding expression must name a type, for Template Non-Type Parameters, the expression must evaluate to a compile-time constant of the appropriate type, and for Template Template Parameters, the expression must name a template with the correct signature. e.g.:
template<typename T,unsigned i> struct FixedArray { T data[i]; }; FixedArray<int,3> a; // array of 3 integers FixedArray<int,1+6/3> b; // array of 3 integers template<template<typename T,typename Allocator> class Container> struct ContainerPair { Container<int,std::allocator<int> > intContainer; Container<std::string,std::allocator<std::string> > stringContainer; }; ContainerPair<std::deque> deqCont; // two std::deques ContainerPair<std::vector> vecCont; // two std::vectors
The second example demonstrates two things. Firstly, the template signature of the Template Template Parameter must exactly match the signature of the template passed as the argument, and standard containers have two template parameters. Secondly, if the last argument of a template parameter list is a template reference, as for Container<int,std::allocator<int> >, then the two closing angle brackets (>) must be separated by a space, to avoid being interpreted as the shift right operator (>>).
Templates requirements and concepts
Templates implicitly impose requirements on their parameters, particularly Template Type Parameters and Template Template Parameters. These requirements generally take the form of operations that must work on objects of the appropriate type, or class members that must exist and refer to objects or types or functions (with the implicit extra requirement that the type referred to is a class). A Concept is a set of requirements that describe a useful feature of a type. For example, the C++ Standard Library makes reference to things being Assignable or Copy-Constructible; these are Concepts that make the following requirements:
- Given a type T, that type is Copy-Constructible if the expression T a(b); is defined, where b is an expression of type T, and the resultant object a has a value equivalent to the value of the expression b. That type is Assignable if the expression a=b; is defined, where a and b are expressions of type T, and the value of the object referred to by the expression a is equivalent to the value of the object referred to by the expression b after the assignment.
- All built-in types are both Copy-Constructible and Assignable. For classes, these requirements translate into requirements on member functions.
- A class T is Copy-Constructible if it has a constructor which can be called with one argument of type const-reference-to-T. This may be a constructor with one argument, or it may be a constructor with more than one argument, with default values provided for the remaining arguments. The object thus constructed should have a value equivalent to that of the argument.
- A class T is Assignable if it defines a copy-assignment operator (operator=()) which has an argument of type T, or const-reference-to-T. The object to which the member function belongs should have a value equivalent to that of the argument after the completion of such a member function.
Template Specialization and Overloading
Template Specialization allows one to decide that for a specific set of template parameters, the code instantiated for a template should be different to the general case. Say, for example, one wish to use a template with a new class as a Template Type Parameter, and though it has the same semantic behaviour as the classes the template was designed for, the syntax is different, so the original template won’t compile with the new class as a parameter. The solution to this problem is specialization. Consider the following template function definition:
template<typename T> void func(T value) { value.add(3); }
This function assumes that the type substituted for the Template Type Parameter T is a class with a member function add that takes a single parameter of a type that can be constructed from an int. Thus the following classes would be OK:
class T1 { public: void add(int i,double d=0.0); }; class T2 { public: std::string add(double d); };
However, the class T3 below is not OK, because Add has a capital A.
class T3 { public: void Add(int i); };
This means that one cannot use the original function template func for T3.
Comparing C# and Java Generics
With Java generics, we don't actually get any of the execution efficiency, because when one compiles a generic class in Java, the compiler takes away the type parameter and substitutes Object everywhere. So the compiled image for List<T> is like a List where it uses the type Object everywhere. Of course, if you now try to make a List<int>, you get boxing of all the ints. So there's a bunch of overhead there. Furthermore, to keep the VM happy, the compiler actually has to insert all of the type casts that were not written by the programmer. If it's a List of Object and we're trying to treat those Objects as Customers, at some point the Objects must be cast to Customers to keep the verifier happy. Java in their implementation does this by automatically inserting those type casts for you. Though this simplifies the programmer's job and avoids cluttering in code, there is no efficiency attained out of this optimization. Java's generics implementation relies on erasure of the type parameter, when we get to runtime, we don't actually have a faithful representation of what we had at compile time. When we apply reflection to a generic List in Java, we can't tell what the List is a List of.
Comparing C# Generics to C++ Templates
To me the best way to understand the distinction between C# generics and C++ templates is this: C# generics are really just like classes, except they have a type parameter. C++ templates are really just like macros, except they look like classes.
The big difference between C# generics and C++ templates shows up in when the type checking occurs and how the instantiation occurs. First of all, C# does the instantiation at runtime. C++ does it at compile time, or perhaps at link time. But regardless, the instantiation happens in C++ before the program runs. That's difference number one. Difference number two is C# does strong type checking when you compile the generic type. For an unconstrained type parameter, like List<T>, the only methods available on values of type T are those that are found on type Object, because those are the only methods we can generally guarantee will exist. So in C# generics, we guarantee that any operation you do on a type parameter will succeed.
C++ is the opposite. In C++, you can do anything you damn well please on a variable of a type parameter type. But then once you instantiate it, it may not work, and you'll get some cryptic error messages. For example, if you have a type parameter T, and variables x and y of type T, and you say x + y, well you had better have an operator+ defined for + of two Ts, or you'll get some cryptic error message. So in a sense, C++ templates are actually untyped, or loosely typed. Whereas C# generics are strongly typed.
References
1. http://www.artima.com/intv/genericsP.html
2. http://www.generic-programming.org
3. Generics in the Java Programming Language
4. http://www.cplusplus.com/doc/tutorial/templates
5. http://osl.iu.edu/publications/prints/2003/comparing_generic_programming03.pdf
6. http://msdn.microsoft.com/en-us/library/ms172192.aspx#Y1524
7. http://www.justsoftwaresolutions.co.uk/articles/intrototemplates.pdf
8. http://www.sgi.com/tech/stl/stl_introduction.html