Consider the first example in the previous tutorial, where we created the Item, Weapon, and Armour classes. The player would need to have a way of obtaining these things, and one way of doing that is by buying them off a merchant with their hard-earned gold, no doubt pillaged from innumerable dead monsters. We might want to restrict each merchant to a single kind of thing, so we would create separate ItemMerchant, WeaponMerchant, and ArmourMerchant classes, each with an array of their corresponding objects that they can sell. Even if we create a base Merchant class for the common functionality and let the others inherit from it, we’ll still have to duplicate a fair bit of code. Instead, we can put C++’s templating system to good use by defining a single Merchant class that caters for all three inherited classes, and still restricts them each to a single inventory object.

Apologies for the length of the code snippet, it needed a lot of commenting! Using the template keyword we define the Merchant class to be a class template, which instead of defining a class defines an outline for what the class would be like, with T acting as a placeholder for a class (or a type such as int). Later in main we implicitly instantiate the template with Item and Weapon as arguments, creating two separate classes which we can then create instances of in the form of the item_merchant and weapon_merchant objects. Note that our class template accepts exactly one argument, and so any use of the Merchant class must too.

The third example can be valid however, because class templates can be defined to have any (fixed) number of arguments.

Additionally a class instantiated from a class template is treated identically to a class that isn’t, including when used as an argument in a class template instantiation. So this is perfectly acceptable (and often useful)

### Function Templates

Class template member functions can use the template arguments passed to their class, but if you just want to create a standalone function that can accept a variety of input classes then it’s a lot of effort to create an entire class just for that function, so instead you can use function templates, which work in a similar way to class templates but apply to single functions.

The syntax for function templates is identical to that of class templates, but also allows for type deduction; if the type of an argument you are passing to a function template is clear, you do not need to include it in the argument list, allowing you to omit the list entirely as we’ve done here. You can still add empty <> if you want to make it clear that the function is an instantiation of a function template, if you wish.

It is important to note that templates will not be compiled if they are never used in the same file that they are defined (the compiler compiles each file individually so it doesn’t know about instantiations in other files). This means that if you declare and define a template in a .cpp and .hpp pair but do not explicitly instantiate it in them, then it will not be useable in any other .cpp file and you will get linker errors.

### Specialisation

It is also possible to explicitly specialise a function template for a given class/type, in which case the specialisation overloads the function as normal. To do this, use the template keyword but don’t pass any arguments to it.

Now when sum is called with a double argument (explicitly or as deduced by the compiler) then this function—which sums the squares of the values—will be called instead.

Specialisation of class templates is also possible, in which case the the specialised class will be used when an instance of that class template is instantiated with the same arguments as the specialisation. It is also possible to specialise individual member functions of class templates, however this prevents you from specialising the entire class template.

### Exercise

Modify the Vec3 class to be a class template, replacing the float type with a general one. When the type is of int, the mag function should return the manhattan length of the vector, defined by $|(x, y, z)| = |x| + |y| + |z|$. (If x is an int then |x| is the positive version of x.) Remember to explicitly instantiate the Vec3 class for at least the int and double types! The following should work with your class.

Solution