Nội dung text TEmplate.pdf
Template
• Write a program to add two numbers.
Generic function • A generic function defines a general set of operations that will be applied to various types of data. • A generic function is created using the keyword template. • template ret-type func-name(parameter list) { // body of function } • Ttype is a placeholder name for a data type used by the function • This name may be used within the function definition. • However, it is only a placeholder that the compiler will automatically replace with an actual data type when it creates a specific version of the function. Although the use of the keyword class to specify a generic type in a template declaration is traditional, you may also use the keyword typename.