Reading notes effective C + + Item 54 Familiarize yourself with the standard library including the TR1

Source: Internet
Author: User
Tags traits

1. Historical Origins of c++0x

The C + + standard-that is, the document and library of the definition language-was approved in 1998. In 2003, a small "fix Bug" version was released. However the standard Committee is still continuing their work, and a "2.0 version" of the C + + standard is expected to be released in 2009 (although all work is likely to be completed by the end of 2007). Until now, the expected year of publishing the next version of C + + has not been determined, which explains why people are calling the next version of C + + the 200x version of the "c++0x"--c++.

C++0x may contain some interesting new language features, but most new C + + features will be published in the form of standard library additions. We already know what the new library features will be because they are already specified in document TR1 (from technical Report 1 of the C + + library workgroup). Before the official release of C++0X, the standard committee retains the right to modify the functions of TR1, but it is unlikely to make any major changes. TR1 heralds the beginning of a new C + + release-we might call it standard C + + 1.1. If you're not familiar with TR1 functionality, you can't be called a effective C + + programmer, because the functionality in TR1 is a benefit for all kinds of libraries and applications.

2. What is in the C++98 standard library?

Before examining what is TR1, it is valuable to review the main parts of the c++98 version of the standard library:

    • Standard Template Library (STL), containing containers (VECTOR,STRING,,MAP, etc.), iterators, algorithms (find,sort,transform, etc.), function objects (less,greater, etc.) There are also different container and function object adapters (STACK,PRIORITY_QUEUE,MEM_FUN,NOT1, etc.)
    • IOStreams, including support for user-defined buffering, internationalized io, and pre-defined objects Cin,cout,cerr and clog.
    • support internationalization , including multi-region capabilities (Multipue active locales). Like type wchar_t (usually 16bits/char) and wstring (a string composed of wchar_t) can facilitate working with Unicode.
    • supports the processing of numeric values , including complex number (complex) templates and pure number arrays (Valarray).
    • exception inheritance systems , including base class exceptions, derived classes Logic_error and Runtime_error, and other classes that inherit from these classes.
    • C89 's standard library . Everything in the 1989 C standard library was also placed in C + +.

If you are unfamiliar with any of the above terms, I suggest you take enough time to see some C + + reference books.

3. What is included in the TR1?

TR1 presents 14 new components (that is, the library feature fragment (pieces)). All are put into the Std namespace, more precisely, in the embedded namespace TR1. The full name of the TR1 component shared_ptr is therefore std::tr1::shared_ptr. In this book, when we discuss the components of the standard library, I usually omit std::, but I always prefix the TR1 component with TR1::.

This book illustrates some of the components in TR1:

    • Smart pointer tr1::shared_ptr and Tr1::weak_ptr. Tr1::shared_ptr behave like built-in pointers, but they track how many tr1::shared_ptr pointers point to an object. This is called the reference count . When the last pointer is destroyed (that is, when the reference count of the object becomes 0), the object is automatically deleted. This is good for preventing resource leaks in non-circular data structures, but if two or more objects contain tr1::shared_ptr, such a ring is formed, and the loop may hold the reference count of each other's objects, and are larger than 0-- Even when the outer pointers of all the rings are destroyed (that is, when the group of objects as a whole cannot be used). The tr1::weak_ptr,tr1::weak_ptr is designed to be used as a cycle-inducing pointer in a non-cyclic tr1::shared_base data structure. There is no reference count in Tr1::weak_ptr. When the last shared_ptr point to the object is destroyed, the object is deleted, even though tr1::weak_ptr still points to the object. However, such pointers are automatically marked as invalid.

Tr1::shared_ptr is probably the most widely used object in TR1. I've also used this book a number of times, including in item 13, where I explained why it's so important. (This book is not weak_ptr used)

    • tr1::function, which can be used to represent any callable entity (for example, any function or function object), as long as the signatures of those entities are consistent with the target signature. If we want to use it to register a callback function, the function uses int as the argument and the return value is string. We can do this:
1 void registercallback (std::string func (int));   // param type is a function 2 // taking an int and 3 // returning a string

The parameter name func is optional, so the registercallback can be declared as follows:

1 void registercallback (std::string (int));   // same as above; Param 2 // name is omitted

Notice here that "std::string (int)" is the function signature. Tr1:;function can make registercallback more flexible, it can accept any callable entity as its argument, and the calling entity uses int or anything that can be converted to int as a parameter, The return value can be either a string or anything that can be converted to a string. Tr1::function uses the target function signature as the template parameter:

1 void RegisterCallback (std::tr1::function<std::string (int) > func); 2 // the Param "func" would 3 // Take any callable entity 4 // With a sig consistent 5 // With "std::string (int)"

This flexibility is very useful and I have already shown it in item 35.

    • Tr1::bind, it can do all the things that the STL binder bind1st and bind2nd can do, even more. Unlike Binders,tr1::bind in Pre-tr1, which can work in const and non-const member functions, arguments passed by reference can be used, and function pointers can be handled without the help of other functions, so the TR1 is called: There is no need to be doped with ptr_fun,mem_fun or mem_fun_ref before bind. Simply put, Tr1::bind is the second-generation binding tool that is far better than the first generation. I have already made an example in item 35.

I divide the remaining TR1 components into two parts. The first section provides a fairly independent function:

  • Hash table is used to implement Set,multiset,map and Multimap. Each new container will simulate an interface that corresponds to the PRE-TR1 part. The most surprising thing about hash table in TR1 is their name: Tr1::unordered_set,tr1::unordered_multiset,tr1::unordered_map and Tr1::unordered_. Multimap These names emphasize that their contents will not be like Set,multiset or Multimap, and the order of the elements in the hash-based container in TR1 is unordered.
  • Regular Expressions , including search and replace functions based on regular expressions on strings, as well as iterations from one matching string to another, and so on.
  • Tuple, which is a generalization of a pair template that already exists in the standard library. The Tr1::tuple object can hold any number of objects compared to a pair object that holds two objects.
  • Tr1::array, in essence, is an "STL" array, which is an array of member functions that support like begin and end. The size of the Tr1::array is fixed at compile time, and the object does not use dynamic memory.
  • TR1::MEM_FN, a uniform way of adapting the syntax of a member function pointer. Just as Tr1::bind the functions of C++98 's bind1st and bind2nd and extending them, TR1::MEM_FN and c++98 functions in Mem_fun are included and extended.
  • Tr1::reference_wrapper, this is a feature that makes a reference behave like an object. This makes it possible to create a behavior that is like holding a referenced container (in fact, a container can contain only objects or pointers.) )
  • The random number generator (generation) function is better than the random function inherited from the C standard library.
  • Mathematical Special Functions (mathematical special function), including Laguerre polynomials, Bessel functions, fully elliptic integrals (complete elliptic integrals), and so on.
  • C99 Compatibility extension , in order to introduce the functions of many new C99 libraries into C + + and design function collections and templates.

The second set of TR1 components consists of support technologies provided for more complex template programming techniques , including template meta-programming (item 48):

    • type traits, which provides a series of trait classes (see item 47) to provide compile-time information for the type. Given the type attribute of a type T,TR1 can reveal whether T is an built-in type, whether a virtual destructor is provided, if it is an empty class (Item 39), whether it can be implicitly converted to another type U, and so on. The type traits in TR1 can also reveal the appropriate alignment problem (alignment) for a type, which provides important information for programmers implementing custom memory allocation functions (Item 50).
    • tr1::result_of, a template used to derive the function return type. When implementing a template, it is important to be able to reference the type of object returned from a function (template) call, but the return type can rely on the parameters of the function in a complex way. Tr1::result_of is used in many places in the TR1.

While some of the features in TR1, especially Tr1::bind and TR1::MEM_FN, are just some of the components of PRE-TR1, TR1 is just an extra addition to the standard library. No TR1 component is a replacement for an existing component, so legacy code built with PRE-TR1 is still valid.

4. Where to find the TR1 implementation

TR1 itself is just a document. In order to use the functionality that it specifies, you need to access the code that implements these features. The code will eventually be bundled with the compiler to publish it, but I wrote this book in 2005, and if you look for the TR1 component in your standard library implementation, there may be some omissions. Fortunately, there are other places to search: 10 of the 14 components of TR1 are based on a free boost library (see item 55), so if you want to understand and TR1 similar functions (tr1-like), this can be a good resource. Here I say "tr1-like", because although many TR1 functions are based on the boost library, there are some local boost features that are not exactly matched to the TR1 specifications. It's possible, but when you read this book, not only did the TR1 component evolved from the Boost library, there was a consistent implementation in boost, but it also provided the implementation of the remaining 4 TR1 components that were not based on boost.

If, as a stopgap, you want to use a library like TR1 in boost, until the compiler is released with the TR1 implementation, you might use a namespace technique. All boost components are in the namespace boost, but the TR1 component will be in the namespace std::tr1. You can tell the compiler to treat the reference to STD::TR1 as a reference to boost. Like this:

1 namespace STD {2namespace//  namespace Std::tr1 is an alias3// For namespace Boost

Technically, this will get you into the undefined behavior field, because as explained in item 25, it is not allowed to add anything to the Std namespace. In practice, it looks like you won't get into any trouble. When your compiler provides their own TR1 implementations, all you need to do is remove the namespace aliases above, and the code referencing STD::TR1 is still valid.

Probably not the most important part of TR1 based on the boost library is the hash table, but the hash tables has been around for many years, and they are named after Hash_set,hash_multiset,hasp_map and Hash_multimap. It is possible that your compiler has included these templates in its own library. If not, use your favorite search engine to search for these names, as you are sure to find some source code, whether commercial or free.

5. Summary
    • The main features of the standard C + + library include Stl,iostream and locales. The C89 standard library is also included.
    • TR1 adds support for smart pointers, generalized function pointers (tr1::function), hash-based containers, regular expressions, and 10 other components.
    • TR1 itself is just a manual. In order to use TR1, you need an implementation. A source code for the TR1 component comes from boost.

Reading notes effective C + + Item 54 Familiarize yourself with the standard library including the TR1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.