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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access