Presentation is loading. Please wait.

Presentation is loading. Please wait.

Miscellaneous C++ Topics

Similar presentations


Presentation on theme: "Miscellaneous C++ Topics"— Presentation transcript:

1 Miscellaneous C++ Topics
Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Miscellaneous C++ Topics

2 Miscellaneous C++ Topics
Outline A note on Preprocessor Wrappers Inline functions Default Arguments Function Templates CS-2303, A-Term 2012 Miscellaneous C++ Topics

3 Preprocessor Wrappers
Exactly as in C Prevent code from being included more than once. #ifndef – “if not defined” Skip this code if it has been included already #define Define a name so this code will not be included again #endif If the header has been included previously Name is defined already and the header file is not included again. Prevents multiple-definition errors Example #ifndef TIME_H #define TIME_H … // code #endif §A12.5 in K&R (Not in Absolute C++) CS-2303, A-Term 2012 Miscellaneous C++ Topics

4 Miscellaneous C++ Topics
Inline Functions Advice to compiler that the body of the function may be substituted for a call to that function Insert the qualifier inline before return type of function Example:– inline int max(int a, int b) { return (a>b) ? a : b; } §7.2 of Absolute C++ CS-2303, A-Term 2012 Miscellaneous C++ Topics

5 Inline Functions (continued)
Reasons:– Reduce function call overhead—especially for small functions in inner loops Takes advantage of compiler’s local optimizations Trade-off of inline functions Multiple copies of the function code are inserted in the program (possibly making the program larger) The compiler may ignore the inline qualifier Typically does so for all but the smallest functions CS-2303, A-Term 2012 Miscellaneous C++ Topics

6 Inline Function Example
inline qualifier Complete function definition so compiler knows how to expand a cube function call into inline code CS-2303, A-Term 2012 Miscellaneous C++ Topics

7 Inline Functions (continued)
Typical usage:– Member function of a class Very simple, short, fast operations Especially in inner loops Especially set and get functions Saves the (non-trivial) overhead of invoking a method, creating activation record, etc. Encourages cleaner, more readable code inline qualifier is strictly advisory Widely used whenever you ask yourself about efficiency of a calling a function vs. accessing a class member directly CS-2303, A-Term 2012 Miscellaneous C++ Topics

8 Miscellaneous C++ Topics
Questions? CS-2303, A-Term 2012 Miscellaneous C++ Topics

9 Miscellaneous C++ Topics
Default Argument Definition:– A default value to be passed to a parameter When the function/method call does not specify an argument for that parameter Example void prnt(int value, int base = 10); … prnt(100); //prints in base 10 prnt(100,16); //prints in base 16 prnt(100,8); //prints in base 8 prnt(100,3); //prints in base 3 An “assignment” as part of parameter list CS-2303, A-Term 2012 Miscellaneous C++ Topics

10 Default Arguments (continued)
Must be rightmost parameter(s) in parameter list of function Default value specified in function prototype Usually in class header file So compiler knows how to compile calls If one argument is defaulted, all arguments to right of it must be defaulted also CS-2303, A-Term 2012 Miscellaneous C++ Topics

11 Default Argument Example
void f(int a, double b, int c = 10, bool d = false); f(100, 3.14) // okay f(100, 3.14, 15) // okay f(100, 3.14, 15, true) // okay f(100, 3.14, , true) // no good f(100, 3.14, 10, true) // okay CS-2303, A-Term 2012 Miscellaneous C++ Topics

12 Summary – Default Arguments
Simplest form of function overloading Default values specified in header and function prototype Defaults applied from right to left §4.2, Absolute C++ (Near end of section) CS-2303, A-Term 2012 Miscellaneous C++ Topics

13 Miscellaneous C++ Topics
Questions? CS-2303, A-Term 2012 Miscellaneous C++ Topics

14 Miscellaneous C++ Topics
Problem What about the case when all of the overloaded functions have essentially the same code, but for different types Not good style to write a bunch of nearly identical code Inefficient of programmer time Various instances can get out of sync with each other Some cases might be missed CS-2303, A-Term 2012 Miscellaneous C++ Topics

15 Solution – Function Templates
A more compact and convenient form of overloading. Identical program logic and operations for each data type §16.1 of Absolute C++ CS-2303, A-Term 2012 Miscellaneous C++ Topics

16 Miscellaneous C++ Topics
Function Template Written by programmer once Essentially defines a whole family of overloaded functions Begins with the template keyword Contains a template parameter list of formal type and the parameters for the function template are enclosed in angle brackets (<>) Formal type parameters Preceded by keyword typename or keyword class Placeholders for fundamental types or user-defined types CS-2303, A-Term 2012 Miscellaneous C++ Topics

17 Function Template Example
Using formal type parameter T in place of data type CS-2303, A-Term 2012 Miscellaneous C++ Topics

18 Miscellaneous C++ Topics
Result Whole Family of Overloaded Functions All with the “same” source code All doing the same thing, but with different data types Defined in one place Common Programming Errors Not placing keyword class or keyword typename before every formal type parameter of a function template Writing < class S, T > instead of < class S, class T > ) is a syntax error CS-2303, A-Term 2012 Miscellaneous C++ Topics

19 Miscellaneous C++ Topics
Definition:– Specialization The automatic generation of a new “overloaded function” from the template as needed I.e., whenever a function template is called with a particular type Example for function template max with type parameter T called with int arguments Compiler detects a max invocation in the program code. int is substituted for T throughout the template definition. This produces function-template specialization max<int> CS-2303, A-Term 2012 Miscellaneous C++ Topics

20 Specialization Example
Compiler:– Suspends what it was doing Generates a new overloaded function from template maximum for type parameter int Compiles this new function Returns to this program and compiles a call to new function Invoking maximum with int arguments CS-2303, A-Term 2012 Miscellaneous C++ Topics

21 Specialization Example (continued)
Invoking maximum with double arguments Compiler does same again, but for parameter type double Invoking maximum with char arguments And again, but for parameter type char CS-2303, A-Term 2012 Miscellaneous C++ Topics

22 Specialization (continued)
Each time that the compiler encounters a use of maximum with a type that it has not seen before … it creates a new instance of the function maximum with new parameter types With new mangled name! CS-2303, A-Term 2012 Miscellaneous C++ Topics

23 Function Templates Summary
Very important for Code readability Code reuse Good coding style Programmer efficiency Foundation of other template features in C++ CS-2303, A-Term 2012 Miscellaneous C++ Topics

24 Miscellaneous C++ Topics
Questions? CS-2303, A-Term 2012 Miscellaneous C++ Topics


Download ppt "Miscellaneous C++ Topics"

Similar presentations


Ads by Google