Template Arguments
An alias is a new name for a template in which some or all of the arguments have already been decided. It is not a copy of the original template, and it is not a new kind of value.
Repeating a Fixed Argument Again and Again
Imagine a template that takes two type arguments. In your program you almost always use the same type for one of them, and the other one changes from place to place. Every time you want a value of that template, you must write out both arguments in full, even though one of them never changes. Repeating the fixed argument is tedious, and it makes the code harder to read. A template alias is the tool for this situation.
What do you think happens?
If you fix one of the two arguments of a template, how many arguments does the new template still need?
Reveal answer
One
Fixing one of the two arguments leaves the other one open. The new template still takes one argument of its own, which is the one you have not fixed.
What an Alias Binds
A template alias defines a new template by binding some or all of the template arguments of an existing template. Binding means that a particular argument is given a fixed value inside the alias, so the person using the alias no longer supplies that argument.
The key word in that definition is binding. Once an argument is bound, it has been decided in advance. The alias does not ask for that argument again. What remains is only the arguments that were left open.
An alias is a new name for a template in which some or all of the arguments have already been decided. It is not a copy of the original template, and it is not a new kind of value.
The diagram shows a template with two argument slots. The first slot has been bound to a fixed type, so it is marked as changed. The second slot is still open, so whoever uses the alias must still supply a value for it. The alias is tied to the original template: the bound slot is part of the original template's definition, and the open slot is still an argument of the original template.
Partial Binding Versus Full Binding
The description allows two cases. In partial binding, only some arguments are bound, so the new template still takes arguments of its own. In full binding, all of the arguments are bound, so nothing is left for the user to supply. Both cases are still aliases, because the new name is defined in terms of the original template with its arguments fixed in place.
Writing an Alias: A Worked Trace
- Pair is the original template, with two arguments named First and Second.
- IntPair is the alias. It binds First to int and leaves Second open.
- IntDoublePair is an alias that binds both arguments, so it needs nothing at the point of use.
Using a Partially Bound Alias
Given the original template Pair with two arguments, and the alias IntPair<Second> = Pair<int, Second>, what does a learner have to supply when using IntPair?
- 1Identify the original template
The original template is Pair, which takes two arguments: First and Second.
- 2Find the bound argument
The alias fixes First to int. The learner no longer supplies First when using the alias.
- 3Find the open argument
Second is not fixed, so it remains a parameter of the new template. The learner must supply it at the point of use.
- 4Use the alias
Writing IntPair<double> fills the open slot with double, giving the full type Pair<int, double>.
The learner supplies exactly one argument, the one that was left open. The bound argument int is already part of the alias.
Notice that the alias is not an independent copy of Pair. It is a name that points back to Pair with one slot already filled. If the original template changes, the alias is still defined in terms of that original template.
Mistakes When Writing or Reading Aliases
Common mistake Treating the alias as a separate copy of the original template
What it looks likeAssuming IntPair defines its own new struct with its own fields.
Why it failsThe source describes an alias as a name tied to the original template, not an independent copy of it.
Do this insteadRead IntPair as another name for Pair with one argument already fixed.
Common mistake Dropping the open argument when using a partially bound alias
What it looks likeWriting IntPair p; when Second has not been bound.
Why it failsPartial binding leaves some arguments open, so the user must still supply those when using the alias.
Do this insteadWrite IntPair<double> p; and supply the open argument.
Common mistake Supplying arguments to a fully bound alias
What it looks likeWriting IntDoublePair<int> when IntDoublePair binds all of its arguments.
Why it failsA fully bound alias has no open arguments left, so there is nothing for the user to supply.
Do this insteadWrite IntDoublePair with no arguments.
Common mistake Misreading which slot is bound
What it looks likeAssuming Second is fixed in IntPair<Second> = Pair<int, Second>, because it appears in the alias heading.
Why it failsAppearing in the alias heading means the argument is still open. The bound argument is the one given a fixed value on the right-hand side.
Do this insteadCheck the right-hand side: arguments with a concrete value such as int are bound, and arguments that still appear as names are open.
Predicting the Open Slots
An original template takes three arguments, A, B, and C. An alias binds A to char and B to long, and leaves C open. How many arguments must a learner supply when using the alias, and which argument are they?
Hints
- Count the bound arguments first, then subtract them from the total.
- The open argument is the one that still appears as a name on the right-hand side of the alias.
Key Takeaways
- A template alias defines a new template by binding some or all of the template arguments of an existing template.
- Binding fixes an argument's value inside the alias, so the user of the alias no longer supplies it.
- Partial binding leaves some arguments open, so the user supplies only those. Binding all arguments leaves nothing to supply at the point of use.
- An alias is a name tied to the original template, not an independent copy of it. Common mistakes include dropping open arguments and misreading which arguments are bound.