Combining this syntax with decltype leads to the following solution for specifying the return type for gt():
template
auto gt(T1 x, T2 y) -> decltype(x + y)
{
...
return x + y;
}
Now decltype comes after the parameter declarations, so x and y are in scope and can be used.
Summary
C++ has expanded C function capabilities. By using an inline keyword with a function definition and by placing that definition ahead of the first call to that function, you suggest to the C++ compiler that it make the function inline. That is, instead of having the program jump to a separate section of code to execute the function, the compiler replaces the function call with the corresponding code inline. An inline facility should be used only when the function is short.
A reference variable is a kind of disguised pointer that lets you create an alias (that is, a second name) for a variable. Reference variables are primarily used as arguments to functions that process structures and class objects. Normally, an identifier declared as a reference to a particular type can refer only to data of that type. However, when one class is derived from another, such as ofstream from ostream, a reference to the base type may also refer to the derived type.
C++ prototypes enable you to define default values for arguments. If a function call omits the corresponding argument, the program uses the default value. If the function includes an argument value, the program uses that value instead of the default. Default arguments can be provided only from right to left in the argument list. Thus, if you provide a default value for a particular argument, you must also provide default values for all arguments to the right of that argument.
A function’s signature is its argument list. You can define two functions having the same name, provided that they have different signatures. This is called
Function templates automate the process of overloading functions. You define a function by using a generic type and a particular algorithm, and the compiler generates appropriate function definitions for the particular argument types you use in a program.
Chapter Review
1. What kinds of functions are good candidates for inline status?
2. Suppose the song() function has this prototype:
void song(const char * name, int times);
a. How would you modify the prototype so that the default value for times is 1?
b. What changes would you make in the function definition?
c. Can you provide a default value of "O, My Papa" for name?
3. Write overloaded versions of iquote(), a function that displays its argument enclosed in double quotation marks. Write three versions: one for an int argument, one for a double argument, and one for a string argument.
4. The following is a structure template:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
a. Write a function that has a reference to a box structure as its formal argument and displays the value of each member.
b. Write a function that has a reference to a box structure as its formal argument and sets the volume member to the product of the other three dimensions.
5. What changes would need be made to Listing 7.15 so that the functions fill() and show() use reference parameters?
6. The following are some desired effects. Indicate whether each can be accomplished with default arguments, function overloading, both, or neither. Provide appropriate prototypes.
a. mass(density, volume) returns the mass of an object having a density of density and a volume of volume, whereas mass(density) returns the mass having a density of density and a volume of 1.0 cubic meters. All quantities are type double.
b. repeat(10, "I'm OK") displays the indicated string 10 times, and repeat("But you're kind of stupid") displays the indicated string 5 times.
c. average(3,6) returns the int average of two int arguments, and average(3.0, 6.0) returns the double average of two double values.
d. mangle("I'm glad to meet you") returns the character I or a pointer to the string "I'm mad to gleet you", depending on whether you assign the return value to a char variable or to a char* variable.
7. Write a function template that returns the larger of its two arguments.
8. Given the template of Chapter Review Question 7 and the box structure of Chapter Review Question 4, provide a template specialization that takes two box arguments and returns the one with the larger volume.
9. What types are assigned to v1, v2, v3, v4, and v5 in the following code (assuming the code is part of a complete program)?
int g(int x);
...
float m = 5.5f;
float & rm = m;
decltype(m) v1 = m;
decltype(rm) v2 = m;
decltype((m)) v3 = m;
decltype (g(100)) v4;
decltype (2.0 * m) v5;
Programming Exercises