1 / 11

C++ Review Templates

C++ Review Templates. Parasol Lab, Texas A&M University. Outline. Generic Programming C++ Templates Function Templates Class Templates Typedef Summary. Generic Programming. Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”

zamora
Télécharger la présentation

C++ Review Templates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Review Templates Parasol Lab, Texas A&M University

  2. Outline • Generic Programming • C++ Templates • Function Templates • Class Templates • Typedef • Summary

  3. Generic Programming • Programming/developing algorithms with the abstraction of types • Algorithms/data is expressed “without type” • The uses of the abstract type define the necessary operations needed when instantiation of the algorithm/data occurs template<typename T> T Add(const T& t1, const T& t2){ return t1+t2; }

  4. C++ Templates • Templates are not types, but rather they are a placeholder for a type • At compile time, the compiler automatically “replaces” the placeholders with the concrete type • Closer to reality – the compiler makes a copy of the template, fills in the placeholders, and compiles the code • C++ templates come in two flavors: • Functions templates • Class templates • Similar to Java Generics

  5. Function Templates • used to define generic algorithms • While this is useful, it only works for integers. • A better solution is to define a function template for max int max(int x, int y){ return x < y ? y : x; } template<class T> T max(T x, T y){ return x < y ? y : x; }

  6. Function Templates • Nothing special has to be done to use a function template • Note: all that is required of the type passed to max is the comparison operator, operator<. int main(intargc, char* argv[]) { int a = 3, b = 7; double x = 3.14, y = 2.71; cout << max(a, b) << endl; // Instantiated with type int cout << max(x, y) << endl; // Instantiated with type double cout << max(a, x) << endl; // ERROR: types do not match }

  7. Class Templates • Class Templates • May contain data member objects of any type. template <class T> class myarray { public: T* v; intsz; myarray(int s) { v = new T [sz = s]; } // Constructor ~myarray() { delete[] v; } // Destructor T& operator[] (inti) { return v[i]; } void set(inti, T val) { v[i] = val; } int size() { return sz; } }; • Then instantiate the same container with objects of different types: myarray<int> iarr(10); myarray<shape> sarr(10);

  8. Typedef • “alias” of types into a short hand • Very common when using templates as syntax can be verbose • Ex. • typedef vector<int> VecInt; • typedef map<string, tuple<double, int, float, MyClass> > MyTuple; • Sometimes you need to use typename before the type • Typedef in a templated function for type located within a templated class • typedeftypename vector<T>::iterator IT;

  9. The skies are the limit! • Templates have an extreme amount of uses • Can have templated classes and functions with many template parameters • Specialized templates for specific types • Etc, etc, etc. template<typenameT1, typenameT2> void myFunc(T1& t1, T2& t2); template<class T> void myFunc(T& t); template<> void myFunc<string>(string& t); //specialization for strings

  10. Summary • Generic programming allows for the abstraction of types • C++ templates are an instantiation of generic programming • C++ has function templates and class templates • Templates have many uses and allow for very interesting code design

  11. Exercise • Create a class Point which has a template parameter of the type of internal data, T. Store an internal array of type T which should have a fixed dimension (2, 3, or n – maybe passed in the constructor). • Create a template function to compute the minimum of an array. Recommendation: template the function on a generic array type • Instantiate a Point<double> and a Point<int> and print their values. Create an array of 10 random integers and an array of 10 random doubles. Use your minimum function to print the minimum of the arrays.

More Related