{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


L15_templates - Templates An introduction to C Templates...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 An introduction to C++ Templates For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Piyush Kumar Templates | Are C macros on Steroids | Give you the power to parametrize | Compile time computation | Performance “The art of programming programs that read, transform, or write other programs.” - François-René Rideau Generic Programming | How do we implement a linked list with a general type inside? z void pointers? z Using macros? z Using Inheritance? Templates | Function Templates | Class Templates | Template templates * | Full Template specialization | Partial template specialization Metaprogramming | Programs that manipulate other programs or themselves | Can be executed at compile time or runtime. | Template metaprograms are executed at compile time. Good old C | C code z Double square(double x) { return x*x; } sqare(3.14) Å Computed at compile time z #define square(x) ((x)*(x)) z Static double sqrarg; # define SQR (a) (sqrarg=(a), sqrarg*sqrarg)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Templates | Help us to write code without being tied to particular type. | Question: How do you swap two elements of any type? How do you return the square of any type? Defining the template | Template parameter T. | Syntax: template < comma separated list of parameters > | For historical reasons, you can use “class” instead of typename. template <typename T> return a < b ? a : b ; } min.hpp Using the template #include <iostream> #include <string> #include “min.hpp” int main(){ int i = 12; std::cout << “min(7,i): “ << ::min(7,i) << std::endl; std::string s1 = “math”; std::string s2 = “cs”; std::cout << “min(s1,s2): “ << ::min(s1,s2) << std::endl; } return a < b ? a : b; } The process of replacing template parameters with concrete types is cal ed instantiation. A compile time error // The following does not provide operator< std::complex<float> c1,c2; min(c1,c2); // error at compile time. Attempt to instantiate a template for a type that doesn’t support all the Operations used within it will result in a compile-time error. Function Templates | C++ z template< typename T > inline T square ( T x ) { return x*x; } z A specialization is instantiated if needed : square<double>(3.14) z Template arguments maybe deduced from the function arguments Æ square(3.14) z MyType m; … ; square(m); Æ expands to square<MyType>(m) Operator * must be overloaded for MyType Function Templates | template<typename T> T tmp(a); // cc required a = b; // ao required b = tmp; } Mytype x = 1111; Mytype y = 100101;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

L15_templates - Templates An introduction to C Templates...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online