Template and STL Changes

C++11 makes several changes extending the usability of templates in general and the Standard Template Library in particular. Some are in the library itself; others relate to ease of use. In this chapter we’ve already mentioned template aliases and the STL-friendly smart pointers.

Range-based for Loop

The range-based for loop (discussed in Chapter 5, “Loops and Relational Expressions,” and Chapter 16) simplifies writing loops for built-in arrays and for classes, such as std::string and the STL containers, that have begin() and end() methods identifying a range. The loop applies the indicated action to each element in the array or container:

double prices[5] = {4.99, 10.99, 6.87, 7.99, 8.49};
for (double x : prices)
    std::cout << x << std::endl;

Here, x takes on the value of each element in prices in turn. The type of x should match the type of the array element. An easier and safer way of doing this is to use auto to declare x; the compiler will deduce the type from the information in the prices declaration:

double prices[5] = {4.99, 10.99, 6.87, 7.99, 8.49};
for (auto x : prices)
    std::cout << x << std::endl;

If your intent is to have the loop modify elements of the array or container, use a reference type:

std::vector<int> vi(6);
for (auto & x: vi)         // use a reference if loop alters contents
    x = std::rand();

New STL Containers

C++11 adds forward_list, unordered_map, unordered_multimap, unordered_set, and unordered_multiset to its collection of STL containers (see Chapter 16). The forward_list container is a singly linked list that can be traversed in just one direction; it’s simpler and more economical of space than the doubly linked list container. The other four containers support implementing hash tables.

C++11 also adds the array template (discussed in Chapter 4, “Compound Types,” and Chapter 16), for which one specifies an element type and a fixed number of elements:

std::array<int,360> ar;  // array of 360 ints

This template class does not satisfy all the usual template requirements. For example, because the size is fixed, you can’t use any method, such as put_back(), that changes the size of a container. But array does have the begin() and end() methods, which allow you to use many of the range-based STL algorithms with array objects.

New STL Methods

C++11 adds cbegin() and cend() to the list of STL methods. Like begin() and end(), the new methods return iterators to the first element and to one past the last element of a container, thus specifying a range encompassing all the elements. In addition, the new methods treat the elements as if they were const. Similarly, crbegin() and crend() are const versions of rbegin() and rend().

More significantly, STL containers now have move constructors and move assignment operators in addition to the traditional copy constructors and regular assignment operators. This chapter describes move semantics later.

valarray Upgrade

The valarray template was developed independently of the STL, and its original design precluded using range-based STL algorithms with valarray objects. C++11 adds two functions, begin() and end(), that each take a valarray argument. They return iterators to the first and one past the last element of a valarray object, allowing one to use range-based STL algorithms (see Chapter 16).

export Departs

C++98 introduced the export keyword in the hopes of creating a way to separate template definitions into interface files containing the prototypes and template declarations and implementation files containing the template function and methods definitions. This proved to be impractical, and C++11 ends that roll for export. However, the Standard retains export as a keyword for possible future use.

Angle Brackets

To avoid confusion with the >> operator, C++ required a space between the brackets in nested template declarations:

std::vector<std::list<int> > vl; // >> not ok

C++11 removes that requirement:

std::vector<std::list<int>> vl; //  >> ok in C++11

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.187.62