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

[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.

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.

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+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

1b)+Show Solution

1c)+Show Solution

1d)+Show Solution

1e)+Show Solution

1f)+Show Solution

1g)+Show Solution

1h)+Show Solution

1i)+Show Solution

1j)+Show Solution

[/quiz] [quiz+number=”2″]

Show Solution

2b)+Building+on+quiz+2a,+explain+why+the+following+statement+will+or+won’t+compile:

Show Solution

[/quiz] [quiz+number=”3″]

3a)+What+is+the+output+of+this+program+and+why?

Show Solution

3b)+Why+won’t+the+following+compile?

Show Solution

3c)+Why+won’t+the+following+compile?

Show Solution

[/quiz] [quiz+number=”4″]

What+is+the+output+of+this+program+and+why?

Show Solution

[/quiz] [quiz+number=”5″]

5a)+Write+a+function+template+named+`add`+that+allows+the+users+to+add+2+values+of+the+same+type.++The+following+program+should+run:

and+produce+the+following+output:

```5
4.6
```

Show Solution

5b)+Write+a+function+template+named+`mult`+that+allows+the+user+to+multiple+one+value+of+any+type+and+an+integer.++The+following+program+should+run:

and+produce+the+following+output:

```6
3.6
```

Show Solution

5c)+Write+a+function+template+named+`sub`+that+allows+the+user+to+subtract+two+values+of+different+types.++The+following+program+should+run:

and+produce+the+following+output:

```1
1.5
2.5
```

Show Solution