Online Book Reader

Home Category

Professional C__ - Marc Gregoire [342]

By Root 1299 0

Inheritance versus Specialization

Some programmers find the distinction between template inheritance and template specialization confusing. The following table summarizes the differences:

INHERITANCE SPECIALIZATION

Reuses code? Yes: Subclasses contain all superclass members and methods. No: You must rewrite all code in the specialization.

Reuses name? No: The subclass name must be different from the superclass name. Yes: The specialization must have the same name as the original.

Supports polymorphism? Yes: Objects of the subclass can stand in for objects of the superclass. No: Each instantiation of a template for a type is a different type.

Use inheritance for extending implementations and for polymorphism. Use specialization for customizing implementations for particular types.

Template Aliases

Chapter 9 introduces the concept of a typedef. It allows you to give another name to specific types. For example you could write the following typedef:

typedef int MyInt;

With the preceding typedef you have given another name to type int allowing you to define integers by using type MyInt instead of int as follows:

MyInt i = 123;

Of course, the original type int still exists and can still be used as type specification. Similarly, you could use a typedef to give another name to a templatized class. However, C++ requires you to specify concrete types for each template type. An example will make it clearer. Suppose you have the following class using templates:

template

class MyTemplateClass {/* ... */};

If you want to use a typedef to define another name for MyTemplateClass, you have to give concrete types for T1 and T2. For example:

typedef MyTemplateClass OtherName;

Specifying only one of the types, like the following example, is not valid in C++:

template

typedef MyTemplateClass OtherName; // Error

C++11 removes this limitation by introducing template aliases, which can be used as follows:

template

using OtherName = MyTemplateClass;

Pay special attention to the syntax. The new type name OtherName should be at the beginning with the template alias syntax, while it should be at the end for the typedef syntax. This template/type alias syntax can be used to replace the earlier typedef syntax, whether a template is involved or not. For example:

using MyInt = int;

This is exactly the same as:

typedef int MyInt;

Alternative Function Syntax

The alternative function syntax is discussed in Chapter 9 and is mentioned again here because it is a very useful C++11 feature in combination with templates. The problem solved with the alternative function syntax is that you don’t always know the exact return type at the beginning of your function prototype. Take the following templatized function as example:

template

RetType myFunc(const Type1& t1, const Type2& t2) {return t1 + t2;}

In this example, RetType should be the type of the expression t1+t2, which isn’t known yet at the beginning of the prototype line. t1 and t2 become known once the semantic analyzer reaches the end of the parameter list. Chapter 1 introduces the C++11 decltype feature. decltype(T) returns the type of its argument T. With this knowledge, you might try to solve the previous return type issue by using the decltype feature as follows:

template

decltype(t1+t2) myFunc(const Type1& t1, const Type2& t2) {return t1 + t2;}

Unfortunately, this is also not valid in C++11 because t1 and t2 are still not yet defined when the compiler is parsing decltype(t1+t2).

C++11 solves this problem with the alternative function syntax as follows. Note that in the new syntax, the return type is specified after the parameter list, hence the names of the parameters (and their types, and consequently the type t1+t2) are known:

template

auto myFunc(const Type1& t1, const Type2& t2) -> decltype(t1+t2)

{return t1 + t2;}

Code snippet from AlternativeFunctionSyntax\AlternativeFunctionSyntax.cpp

Return Main Page Previous Page Next Page

®Online Book Reader