Search

8.14 — Function template instantiation

In the previous lesson (%Failed lesson reference, id XX%), we introduced function templates, and converted a normal max() function into a max<T> function template:

In this lesson, we’ll focus on how function templates are used.

Using a function template

Function templates are not actually functions — their code isn’t compiled . . . → Read More: 8.14 — Function template instantiation

8.10 — Function overload differentiation

In the prior lesson (8.9 — Introduction to function overloading), we introduced the concept of function overloading, allows us to create multiple functions with the same name, so long as each identically named function has different parameters (or the functions can be otherwise differentiated).

In this lesson, we’ll talk about how overloaded functions are differentiated. . . . → Read More: 8.10 — Function overload differentiation

8.x+–+Chapter+8+summary+and+quiz

test+You+made+it!++The+topics+in+this+chapter+(particularly+type+aliases,+overloaded+functions,+and+function+templates)+appear+everywhere+in+the+C+++standard+library.++We’ve+got+one+more+chapter+to+go+(introducing+compound+types),+and+then+we’ll+be+ready+to+dig+into+some+of+the+most+useful+pieces+of+the+standard+library!

[section+top]Quick+review[/section]

The+process+of+converting+a+value+from+one+data+type+to+another+data+type+is+called+a+type+conversion.

Implicit+type+conversion+(also+called+automatic+type+conversion+or+coercion)+is+performed+whenever+one+data+type+is+expected,+but+a+different+data+type+is+supplied.+If+the+compiler+can+figure+out+how+to+do+the+conversion+between+the+two+types,+it+will.+If+it+doesn’t+know+how,+then+it+will+fail+with+a+compile+error.

The+C+++language+defines+a+number+of+built-in+conversions+between+its+fundamental+types+(as+well+as+a+few+conversions+for+more+advanced+types)+called+standard+conversions.++These+include+numeric+promotions,+numeric+conversions,+and+arithmetic+conversions.

A+numeric+promotion+is+the+conversion+of+smaller+numeric+types+to+larger+numeric+types+(typically+int+or+double),+so+that+the+CPU+can+operate+on+data+that+matches+the+natural+data+size+for+the+processor.++Numeric+promotions+include+both+integral+promotions+and+floating-point+promotions.++Numeric+promotions+are+value-preserving,+meaning+there+is+no+loss+of+value+or+precision.

A+Numeric+conversion+is+a+type+conversions+between+fundamental+types+that+isn’t+a+numeric+promotions.++A+narrowing+conversion+is+a+numeric+conversion+that+may+result+in+the+loss+of+value+or+precision.

In+C++,+certain+binary+operators+require+that+their+operands+be+of+the+same+type.+If+operands+of+different+types+are+provided,+one+or+both+of+the+operands+will+be+implicitly+converted+to+matching+types+using+a+set+of+rules+called+the+usual+arithmetic+conversions.

Explicit+type+conversion+is+performed+when+the+programmer+explicitly+requests+conversion+via+a+cast.++A+cast+represents+an+request+by+the+programmer+to+do+an+explicit+type+conversion.++C+++supports+5+types+of+casts:+C-style+casts,+static+casts,+const+casts,+dynamic+casts,+and+reinterpret+casts.++Generally+you+should+avoid+C-style+casts,+const+casts,+and+reinterpret+casts.++static_cast+is+used+to+convert+a+value+from+one+type+to+a+value+of+another+type,+and+is+by+far+the+most+used-cast+in+C++.

Typedefs+and+Type+aliases+allow+the+programmer+to+create+an+alias+for+a+data+type.++These+aliases+are+not+new+types,+and+act+identically+to+the+aliased+type.++Typedefs+and+type+aliases+do+not+provide+any+kind+of+type+safety,+and+care+needs+to+be+taken+to+not+assume+the+alias+is+different+than+the+type+it+is+aliasing.

The+auto+keyword+has+a+number+of+uses.++First,+auto+can+be+used+to+do+type+deduction+(also+called+type+inference),+which+will+deduce+a+variable’s+type+from+its+initializer.++Type+deduction+drops+const+and+references,+so+be+sure+to+add+those+back+if+you+want+them.

Auto+can+also+be+used+as+a+function+return+type+to+have+the+compiler+infer+the+function’s+return+type+from+the+function’s+return+statements,+though+this+should+be+avoided+for+normal+functions.++Auto+is+used+as+part+of+the+trailing+return+syntax.

Function+overloading+allows+us+to+create+multiple+functions+with+the+same+name,+so+long+as+each+identically+named+function+has+different+set+of+parameter+types+(or+the+functions+can+be+otherwise+differentiated).++Such+a+function+is+called+an+overloaded+function+(or+overload+for+short).++Return+types+are+not+considered+for+differentiation.++

When+resolving+overloaded+functions,+if+an+exact+match+isn’t+found,+the+compiler+will+favor+overloaded+functions+that+can+be+matched+via+numeric+promotions+over+those+that+require+numeric+conversions.++When+a+function+call+is+made+to+function+that+has+been+overloaded,+the+compiler+will+try+to+match+the+function+call+to+the+appropriate+overload+based+on+the+arguments+used+in+the+function+call.++This+is+called+overload+resolution.

An+ambiguous+match+occurs+when+the+compiler+finds+two+or+more+functions+that+can+match+an+function+call+to+an+overloaded+functions+and+can’t+determine+which+one+is+best.

A+default+argument+is+a+default+value+provided+for+a+function+parameter.++Parameters+with+default+arguments+must+always+be+the+rightmost+parameters,+and+they+are+not+used+to+differentiate+functions+when+resolving+overloaded+functions.

Function+templates+allow+us+to+create+a+function-like+definition+that+serves+as+a+pattern+for+creating+related+functions.++In+a+function+template,+we+use+template+types+as+placeholders+for+any+types+we+want+to+be+specified+later.++The+syntax+that+tell+the+compiler+we’re+defining+a+template+and+declares+the+template+types+is+called+a+template+parameter+declaration.++

The+process+of+creating+functions+(with+specific+types)+from+function+templates+(with+template+types)+is+called+function+template+instantiation+(or+instantiation)+for+short).++When+this+process+happens+due+to+a+function+call,+it’s+called+implicit+instantiation.++An+instantiated+function+is+called+a+function+instance+(or+instance+for+short),+or+sometimes+a+template+function).

Template+argument+deduction+allows+the+compiler+to+deduce+the+actual+type+that+should+be+used+to+instantiate+a+function+from+the+arguments+of+the+function+call.++Template+argument+deduction+does+not+do+type+conversion.

Template+types+are+sometimes+called+generic+types,+and+programming+using+templates+is+sometimes+called+generic+programming.

When+the+auto+keyword+is+used+as+a+parameter+type+in+a+normal+function,+the+compiler+will+automatically+convert+the+function+into+a+function+template+with+each+auto+parameter+becoming+an+independent+template+type+parameter.+This+method+for+creating+a+function+template+is+called+an+abbreviated+function+templates.

[section+quiz] [quiz+number=”1″]

What+type+of+conversion+happens+in+each+of+the+following+cases?++Valid+answers+are:+No+conversion+needed,+numeric+promotion,+numeric+conversion,+won’t+compile+due+to+narrowing+conversion.++Assume+int+and+long+are+both+4+bytes.

1a)+Show Solution

No+conversion+needed

1b)+Show Solution

Integral+promotion+of+char+’a’+to+int

1c)+Show Solution

Won’t+compile+due+to+narrowing+conversion

1d)+Show Solution

Numeric+promotion+of+bool+true+to+int

1e)+Show Solution

Explicit+numeric+conversion+of+double+5.4+to+int

1f)+Show Solution

Numeric+promotion+of+float+to+double

1g)+Show Solution

Numeric+conversion+of+int+to+double

1h)+Show Solution

Numeric+conversion+of+int+to+long+(this+conversion+is+trivial,+but+it’s+still+a+conversion)

1i)+Show Solution

Won’t+compile+due+to+narrowing+conversion+from+double+to+float

1j)+Show Solution

Numeric+conversion+of+double+to+float+(this+is+allowed+since+5.0+is+constexpr+and+fits+in+the+range+of+the+float) . . . → Read More: 8.x+–+Chapter+8+summary+and+quiz

8.15 — Function templates with multiple template types

In lesson 8.13 — Function templates, we wrote a function template to calculate the maximum of two values:

Now consider the following similar program:

You may be surprised to find that this program won’t compile. Instead, the compiler will issue a bunch of (probably crazy looking) error messages. On Visual Studio, the author . . . → Read More: 8.15 — Function templates with multiple template types

8.4 — Arithmetic conversions

In lesson 5.1 — Operator precedence and associativity, we discussed how expressions are evaluated according to the precedence and associativity of their operators.

Consider the following expression:

When binary operator+ is invoked, it is given two operands, both of type int. Because both operands are the same type, that type will be used . . . → Read More: 8.4 — Arithmetic conversions

8.3 — Numeric conversions

In the previous lesson (8.2 — Floating-point and integral promotion), we covered numeric promotions, which are conversions of specific narrower numeric types to wider numeric types (typically int or double) that can be processed efficiently.

C++ supports another category of numeric type conversions, called numeric conversions, that cover additional type conversions not covered by the . . . → Read More: 8.3 — Numeric conversions

8.2 — Floating-point and integral promotion

In lesson 4.3 — Object sizes and the sizeof operator, we noted that C++ has minimum size guarantees for each of the fundamental types. However, the actual size of these types can vary based on the compiler and architecture.

This variability was allowed so that the int and double data types could be set to . . . → Read More: 8.2 — Floating-point and integral promotion

8.8 — Type deduction for functions

Consider the following program:

When this function is compiled, the compiler will determine that x + y evaluates to an int, then ensure that type of the return value matches the declared return type of the function (or that the return value type can be converted to the declared return type).

Since the compiler . . . → Read More: 8.8 — Type deduction for functions

8.11 — Overloaded function resolution

In the previous lesson (8.10 — Function overload differentiation), we discussed which attributes of a function are used to differentiate overloaded functions from each other. If an overloaded function is not properly differentiated from the other overloads of the same name, then the compiler will issue a compile error.

However, having a set of differentiated . . . → Read More: 8.11 — Overloaded function resolution

6.5 — Variable shadowing (name hiding)

Each block defines its own scope region. So what happens when we have a variable inside a nested block that has the same name as a variable in an outer block? When this happens, the nested variable “hides” the outer variable in areas where they are both in scope. This is called name hiding or . . . → Read More: 6.5 — Variable shadowing (name hiding)