Online Book Reader

Home Category

Professional C__ - Marc Gregoire [344]

By Root 1550 0
the char* specialization

Code snippet from FunctionTemplate\FindTemplateSpecialization.cpp

Function Template Overloading

You can also overload template functions with non-template functions. For example, instead of writing a Find() template specialization for char*, you could write a non-template Find() function that works on char*s:

size_t Find(char*& value, char** arr, size_t size)

{

cout << "overload" << endl;

for (size_t i = 0; i < size; i++) {

if (strcmp(arr[i], value) == 0) {

return i; // Found it; return the index

}

}

return NOT_FOUND; // Failed to find it; return NOT_FOUND

}

Code snippet from FunctionTemplate\FindTemplateOverload.cpp

This function is identical in behavior to the specialized version in the previous section. However, the rules for when it is called are different:

char* word = "two";

char* arr[] = {"one", "two", "three", "four"};

size_t sizeArr = sizeof(arr) / sizeof(arr[0]);

size_t res;

res = Find(word, arr, sizeArr); // Calls the Find template with T=char*

res = Find(word, arr, sizeArr); // Calls the Find non-template function!

Code snippet from FunctionTemplate\FindTemplateOverload.cpp

Thus, if you want your function to work both when char* is explicitly specified and via deduction when it is not, you should write a specialized template version instead of a non-template, overloaded version.

Like template class method definitions, function template definitions (not just the prototypes) must be available to all source files that use them. Thus, you should put the definitions in header files if more than one source file uses them.

Function Template Overloading and Specialization Together

It’s possible to write both a specialized Find() template for char*s and a stand-alone Find() function for char*s. The compiler always prefers the non-template function to a templatized version. However, if you specify the template instantiation explicitly, the compiler will be forced to use the template version:

char* word = "two";

char* arr[] = {"one", "two", "three", "four"};

size_t sizeArr = sizeof(arr) / sizeof(arr[0]);

size_t res;

res = Find(word, arr, sizeArr); // Calls the char* specialization of // the template

res = Find(word, arr, sizeArr); // Calls the Find non-template function

Code snippet from FunctionTemplate\FindTemplateSpecialOverload.cpp

Friend Function Templates of Class Templates

Function templates are useful when you want to overload operators in a class template. For example, you might want to overload the insertion operator (operator<<) for the Grid class template to stream a grid.

If you are unfamiliar with the mechanics for overloading operator<<, consult Chapter 18 for details.

As discussed in Chapter 18, you can’t make operator<< a member of the Grid class: It must be a stand-alone function template. The definition, which should go directly in Grid.h, looks as follows:

template

ostream& operator<<(ostream& ostr, const Grid& grid)

{

for (size_t i = 0; i < grid.mHeight; i++) {

for (size_t j = 0; j < grid.mWidth; j++) {

// Add a tab between each element of a row.

ostr << grid.mCells[j][i] << "\t";

}

ostr << std::endl; // Add a newline between each row.

}

return ostr;

}

Code snippet from FriendFunctionTemplates\Grid.h

This function template will work on any Grid, as long as there is an insertion operator for the elements of the grid. The only problem is that operator<< accesses protected members of the Grid class. Therefore, it must be a friend of the Grid class. However, both the Grid class and the operator<< are templates. What you really want is for each instantiation of operator<< for a particular type T to be a friend of the Grid template instantiation for that type. The syntax looks like this:

// Forward declare Grid template.

template class Grid;

// Prototype for templatized operator<<.

template

ostream& operator<<(ostream& ostr, const Grid& grid);

template

class Grid

{

public:

// Omitted for brevity

friend ostream& operator<< (ostream&

Return Main Page Previous Page Next Page

®Online Book Reader