The noexcept keyword is used to indicate that a function doesn’t throw an exception. It also can be used as an operator that determines whether its operand (an expression) potentially could throw an exception. It returns false if the operand could throw an exception and true otherwise. For example, consider the following declarations:

int hilt(int);

int halt(int) noexcept;

The expression noexcept(hilt) would evaluate as false, as the declaration of hilt() doesn’t guarantee that an exception won’t be thrown. But noexcept(halt) will evaluate as true.

F. The string Template Class

Much of this appendix is a bit technical. However, if you just want to know the capabilities of the string template class, you can concentrate on the descriptions of the various string methods.

The string class is based on a template definition:

template,

                 class Allocator = allocator >

class basic_string {...};

Here charT represents the type stored in the string. The traits parameter represents a class that defines necessary properties that a type must possess to be represented as a string. For example, it should have a length() method that returns the length of a string, represented as an array of type charT. The end of such an array is indicated by the value charT(0), the generalization of the null character. (The expression charT(0) is a type cast of 0 to type charT. It could be just 0, as it is for type char, or more generally, it could be an object created by a charT constructor.) The class also includes methods for comparing values, and so on. The Allocator parameter represents a class to handle memory allocation for the string. The default allocator template uses new and delete in the standard ways.

There are four predefined specializations:

typedef basic_string string;

typedef basic_string u16string;

typedef basic_string u32string;

typedef basic_string wstring;

These specializations, in turn, use the following specializations:

char_traits

allocator

char_traits

allocator

char_traits

allocator

char_traits

allocator

You can create a string class for some type other than char or wchar_t by defining a traits class and using the basic_string template.

Thirteen Types and a Constant

The basic_string template defines several types that are used later in defining the methods:

typedef traits                                traits_type;

typedef typename traits::char_type            value_type;

typedef Allocator                             allocator_type;

typedef typename Allocator::size_type         size_type;

typedef typename Allocator::difference_type   difference_type;

typedef typename Allocator::reference         reference;

typedef typename Allocator::const_reference   const_reference;

typedef typename Allocator::pointer           pointer;

typedef typename Allocator::const_pointer     const_pointer;

Note that traits is a template parameter that corresponds to some specific type, such as char_traits; traits_type becomes a typedef for that specific type. The following notation means that char_type is a type name defined in the class represented by traits:

typedef typename traits::char_type          value_type;

The keyword typename is used to tell the compiler that the expression traits::char_type is a type. For the string specialization, for example, value_type is char.

size_type is used like size_of, except that it returns the size of a string in terms of the stored type. For the string specialization, that would be in terms of char, in which case size_type is the same as size_of. It is an unsigned type.

difference_type is used to measure the distance between two elements of a string, again in units corresponding to the size of a single element. Typically, it would be a signed version of the type underlying size_type.

For the char specialization, pointer is type char *, and reference is type char &. However, if you create a specialization for a type of your own design, these types (pointer and reference) could refer to classes that have the same properties as the more basic pointers and references.

To allow Standard Template Library (STL) algorithms to be used with strings, the template defines some iterator types:

typedef (models random access iterator)         iterator;

typedef (models random access iterator)         const_iterator;

typedef std::reverse_iterator         reverse_iterator;

typedef std::reverse_iterator   const_reverse_iterator;

The template defines a static constant:

static const size_type npos = -1;

Because size_type is unsigned, assigning the value -1 actually amounts to assigning the largest possible unsigned value to npos. This value corresponds to one greater than the largest possible array index.

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги