Concepts / Class Templates

Class Templates

A template alias defines a new template by binding some or all of the template arguments of an existing template.

  • Programming

Why Repeating a Fixed Argument Gets Tedious

Imagine a template that takes two type arguments, but in your program you almost always use the same type for one of them. Every time you want a value of that template, you must write out both arguments, 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 a way to deal with exactly this situation.

What do you think happens?

If you fix one of two template arguments, how many arguments does the new template still need when you use it?

  • One

  • Two

  • None

Reveal answer
Answer:

One. Fixing one of the two arguments leaves the other open, so the user still supplies that one.

The alias binds the fixed argument in advance, so the user supplies only the argument that remains open. Keep this answer in mind as you read about partial binding.

What Binding Means

Definition

A template alias defines a new template by binding some or all of the template arguments of an existing template.

The key word in that definition is binding. Binding means that a particular argument is given a fixed value inside the alias, so the learner no longer supplies it when using the alias. Once an argument is bound, the alias has already decided it.

An alias is not a copy of the original template, and it is not a new kind of value. It is a new name for a template in which some or all of the arguments have already been decided. The alias stays tied to the original template, so it is a name that refers back to that template rather than an independent piece of code.

Partial Binding Versus Full Binding

The description of a template alias allows two cases. In both, the alias is defined in terms of the original template with its arguments fixed in place. The cases differ in how many arguments are left open for the user. The next two subsections look at each case.

First parameterbound to intSecond parameteropen, user supplies it
Which template parameters are fixed by the alias and which remain open for the user to supply? Each argument position maps to either a bound value or an open slot.
First parameterbound to intSecond parameterbound to double
When all arguments are bound, no positions remain open for the user to supply.

Writing an Alias for a Pair Template

The source introduces the idea of an alias with an example but does not reproduce that example in the material available. The code below is an illustrative example written for this article. It follows the rule stated in the source: one argument of an existing template is bound, and the rest stays open. The original template takes two type arguments, and the alias fixes the first one to int.

cpp
Notes on the code
  • Pair is the original class template with two open parameters, First and Second.
  • IntPair is the alias. Its parameter list contains only Second, because First has been bound to int.
  • IntPair<double> supplies the one remaining argument, so p is a Pair<int, double>.
Pairtwo parameters: First,SecondIntPairone parameter: Seconddefined in terms of, with First bound to int
How does the original template with its full parameter list relate to the alias with fewer parameters, and which original parameters does the alias carry forward?
Worked example

Using the partially bound alias

Given Pair and the alias IntPair, what type does the declaration IntPair<double> p create?

  1. 1
    Identify the alias

    IntPair is the new name defined from Pair, with First already bound to int.

  2. 2
    Find the open argument

    Only Second remains open, so the user supplies one argument, here double.

  3. 3
    Combine bound and supplied arguments

    The bound int fills First, and the supplied double fills Second, giving Pair<int, double>.

Result

p has the type Pair<int, double>.

Anatomy of an Alias Declaration

A template alias declaration has several parts. The template parameter list names the arguments the alias still leaves open. The alias name is the new name for the template. The bound arguments are the values fixed inside the alias. The target template is the original template being referred to. Reading the declaration means identifying each part and asking what role it plays.

template <typenameSecond>template parameter list:open argumentusing IntPairalias nameintbound argumentPairtarget template
What are the parts of a template alias declaration, and what role does each play?

Mistakes When Writing or Reading Aliases

  • Common mistake

    Treating the alias as a copy of the original template

    Why it fails

    The source says an alias is not a copy of the original template. It is a name tied to that template, with some or all arguments already decided.

    Do this instead

    Read the alias as a new name for the original template with fixed arguments.

  • Common mistake

    Omitting the argument that is still open

    What it looks like

    IntPair p; when IntPair still has one open parameter, Second

    Why it fails

    After partial binding, the open argument is still required, and the alias does not supply it.

    Do this instead

    Supply the open argument, as in IntPair<double> p.

  • Common mistake

    Assuming an argument can be bound to any position

    What it looks like

    Reading IntPair<double> as Pair<double, int>

    Why it fails

    The bound int is fixed in the first position, so the single supplied argument fills the remaining open position.

    Do this instead

    Check which parameter the alias binds and which it leaves open before reading a use of the alias.

  • Common mistake

    Forgetting that a fully bound alias takes no arguments at the point of use

DeclarationOpen argumentsHow it is used
IntPair with Second openOne (Second)IntPair<double> p;
IntDoublePair with all boundNoneIntDoublePair q;
Incorrect use of IntPairOne still requiredIntPair p; leaves Second unsupplied

Partially bound and fully bound aliases compared with an incorrect use

When Every Argument Is Bound

When an alias binds every argument of the original template, the alias needs nothing from the user. The example below binds both arguments of Pair to fixed types. The alias is still defined in terms of Pair, and it is still a name rather than a copy.

cpp
Notes on the code
  • Both arguments of Pair are bound, so IntDoublePair has no open arguments.
  • The declaration IntDoublePair q; takes no template arguments at the point of use.

Trying a Partial Alias

MEDIUM

Using the Pair template from this article, write an alias named DoubleFirst that binds the second parameter to double and leaves the first parameter open. Then write one declaration that uses DoubleFirst with an int for the open argument.

Hints
  • Count the parameters of Pair. Decide which one stays open and which one is bound.
  • The template parameter list of your alias should contain only the open parameter.
  • The use of the alias should supply exactly one argument, the one for the open position.

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 no longer supplies it at the point of use.
  • Partial binding leaves some arguments open and the user supplies only those; full binding leaves none open.
  • An alias is a new name tied to the original template, not an independent copy of it.
  • The source does not describe how the compiler processes an alias internally, so this article makes no claim about that process.

Related concepts

More concepts starting with C