CS205-2020 Spring - Lecture 7 PDF
CS205-2020 Spring - Lecture 7 PDF
Language
CS205 Spring
Feng Zheng
2020.04.02
Content
• Brief Review
• Reference Variable
• Function Overloading
• Function Template
• Summary
Brief Review
Content of Last Class
• Various function applications
Ø Arrays
Ø C-style strings
Ø Structure
Ø String class and array objects
Ø Recursion
Ø Pointer to functions
Adventures in Functions
Mechanism of Function Call
• Codes are the data as well
Ø The product of the compilation process is an executable program
ü Consist of a set of machine language instructions
Ø The operating system loads these instructions into the memory
ü Each instruction has a particular memory address
ü Jump backward or forward to a particular address (loop or branching)
• Normal function: Jump forth and back
Ø Store the memory address of the instruction immediately following the
function call
Ø Copy function arguments to the stack
Ø Jump to the memory location that marks the beginning of the function
Ø Execute the function code
Ø Jump back to the instruction whose address it saved
C++ Inline Functions
• Compiler replaces the function call with
the corresponding function code
Ø Run a little faster than regular functions
Ø Come with a memory penalty
Ø Be selective about using inline functions
• Two steps
Ø Preface the declaration with the
keyword inline.
Ø Preface the function definition with the
keyword inline
• Inline versus macros
Ø Macros don’t pass by value
Reference Variables
• A new compound type to the language—the reference variable
Ø A reference is a name that acts as an alias
Ø Declare references
ü int & means reference-to int
ü The reference declaration allows you to use two variables interchangeably
ü Both refer to the same value and the same memory location
• Template limitations
Ø It’s easy to write a template function that cannot handle certain types
Ø In some cases, require different codes but the arguments would be the
same
Specializations
• Explicit specializations
Ø If the compiler finds a specialized definition that exactly matches a function
call, it uses that definition without looking for templates.
• Third-Generation Specialization
Ø A function name can have a non template function, a template function, and an
explicit specialization template function, along with all overloaded versions
Ø The prototype and definition for an explicit specialization should be preceded
by template <> and should mention the specialized type by name
Ø A specialization overrides the regular template, and a non template function
overrides both
Ø Run program example 9
Instantiations and Specializations
• Instantiation
Ø Template: merely a plan for
generating a function definition
Ø Instantiation: use the template
to generate a function
definition
ü Implicit: the compiler deduces the
necessity for making the definition
ü Explicit: using the <> notation to
indicate the type and prefixing the
declaration with the keyword
template
Which Function Version Does the
Compiler Pick?
• Multiple functions of the same name
Ø Include: function overloading, function templates, and function
template overloading……
• Overload resolution
Ø Phase 1—Assemble a list of candidate functions
Ø Phase 2—From the candidate functions, assemble a list of feasible
functions
ü Correct number of arguments
ü An exact match for each type of actual argument to the type of the corresponding
formal argument
Ø Phase 3—Determine whether there is a best viable function
Exact Matches and Best Matches
• C++ allows some “trivial conversions” when
making an exact match
Ø If there’s just one, that function is chosen
Ø If more than one are tied, but only one is a non
template function, that non template function is chosen
Ø If more than one candidate are tied and all are
template functions, but one template is more
specialized than the rest, that one is chosen
• Error
Ø If there are two or more equally good non template
functions
Ø If there are two or more equally good template
functions, none of which is more specialized than the
rest
Ø If there are no matching calls, that is also an error
Summary
• Inline function
• Reference variables
• Functions of the same name
Ø Default arguments
Ø Function overloading
Ø Function template
Thanks