Explicit Specializations

Suppose you define a structure like the following:

struct job{      char name[40];      double salary;      int floor;};

Also suppose you want to be able to swap the contents of two such structures. The original template uses the following code to effect a swap:

temp = a;a = b;b = temp;

Because C++ allows you to assign one structure to another, this works fine, even if type T is a job structure. But suppose you only want to swap the salary and floor members, keeping the name members unchanged. This requires different code, but the arguments to Swap() would be the same as for the first case (references to two job structures), so you can’t use template overloading to supply the alternative code.

However, you can supply ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.