Sunday, October 18, 2009

21.6. Boost Libraries in Technical Report 1 (TR1)



21.6. Boost Libraries in Technical Report
1 (TR1)


Several Boost libraries have been
accepted as part of Technical Report 1 (TR1). TR1 is a description of proposed
changes and additions to the C++ Standard Library. GCC (GNU Compiler Collection)
provides a partial implementation of TR1; the site gcc.gnu.org/onlinedocs/libstdc++/ext/tr1.html overviews the TR1 features currently supported.
GCC, which is a part of the GNU project, provides free compilers for several
programming languages, including C, C++ and Java. Boost has also released a
subset of libraries that implement most of the TR1 functionality—this subset is
included in the latest release of the Boost libraries. Here we introduce the
Boost libraries that the corresponding TR1 extensions are based on. Later, we
provide code examples for some of the libraries.


Array[2]



[2] Documentation for
Boost.Array, Nicolai Josuttis, www.boost.org/doc/html/array.html.


Boost.Array is a wrapper for fixed-size arrays that enhances built-in
arrays by supporting most of the STL container interface described in Section
20.1. Boost.Array allows you to use fixed-size
arrays in STL applications rather than vectors
(dynamically sized arrays), which are not as efficient when there is no need for
dynamic resizing.


Bind[3]



[3] Documentation for
Boost.Bind, Peter Dimov, www.boost.org/libs/bind/bind.html.


Boost.Bind
extends the functionality of the standard functions std::bind1st and
std::bind2nd. The bind1st and bind2nd functions are used to adapt binary functions (i.e.,
functions that take two arguments) to be used with the standard algorithms which
take unary functions (i.e., functions that take one argument).
Boost.Bind enhances that functionality by
allowing you to adapt functions that take up to nine arguments.
Boost.Bind also makes it easy to reorder the
arguments passed to the function using placeholders.


Function[4]



[4] Documentation for
Boost.Function, Douglas Gregor, www.boost.org/doc/html/function.html.


Boost.Function allows you to store function pointers, member-function
pointers and function objects in a function wrapper. You can also store a
reference to a function object using the ref and
cref functions added to the <utility> header. This allows you to avoid expensive copy
operations. A boost::function can hold any
function whose arguments and return type can be converted to match the signature
of the function wrapper. For example, if the function wrapper was created to
hold a function that takes a string and returns a string, it
can also hold a function that takes a char* and returns a
char*, because a char* can be converted to a string,
using a conversion constructor.


Mem_fn[5]



[5] Documentation for
Boost.Mem_fn, Peter Dimov, www.boost.org/libs/bind/mem_fn.html.


Boost.mem_fn
enhances the std::mem_fun and std::mem_fun_ref functions.
mem_fun and mem_fun_ref take a
pointer to a member function or a reference to a member function, respectively,
and create a function object that calls that member function. The member
function can take no arguments or one argument. Function objects are often used
with the Standard Library for_each function
and other STL algorithms. We discussed function objects in Section
20.7. Boost.mem_fn enhances the standard
functions by allowing you to create the function object with a pointer,
reference or smart pointer (Section
21.8) to a member function. It also allows the
member functions to take more than one argument. mem_fn is a more flexible version of mem_fun and
mem_fun_ref.


Random[6]



[6]
Jens Maurer, "A Proposal to Add an Extensible Random Number Facility to the
Standard Library," Document Number N1452, April 10, 2003, www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1452.html.


Boost.Random allows you to create a variety of random number generators
and random number distributions. The std::rand and
std::srand functions in the C++ Standard Library
generate pseudo-random numbers. A pseudo-random
number generator
uses an initial state to produce
seemingly random numbers—using the same initial state produces the same sequence
of numbers. The rand function always uses
the same initial state, therefore it produces the same sequence of numbers every
time. The function srand allows you to set the
initial state to vary the sequence. Pseudo-random numbers are often used in
testing—the predictability enables you to confirm the results.
Boost.Random provides pseudo-random number
generators as well as generators that can produce nondeterministic random numbers—a set of random numbers that can't be predicted. Such
random number generators are used in simulations and security scenarios where
predictability is undesirable.


Boost.Random also allows you
to specify the distribution of the numbers generated. A common distribution is
the uniform distribution, which assigns the same probability to each number
within a given range. This is similar to rolling a die or flipping a coin—each
possible outcome is equally as likely. You can set this range at compile time.
Boost.Random allows you to use a distribution in
combination with any random number generator and even create your own
distributions.


Ref[7]



[7]
Documentation for Boost.Ref, Jaakko Järvi, Peter Dimov, Douglas Gregor and Dave
Abrahams, www.boost.org/doc/html/ref.html.


The Boost.Ref library provides reference wrappers that enable you to
pass references to algorithms that normally receive their arguments by value.
The reference_wrapper object contains the reference and allows the
algorithm to use it as a value. Using references instead of values improves
performance when passing large objects to an algorithm.


Regex[8]



[8] Documentation for
Boost.Regex, John Maddock, www.boost.org/libs/regex/doc/index.html.


Boost.Regex provides support for processing regular
expressions in C++. Regular expressions are used to match specific character
patterns in text. Many modern programming languages have built-in support for
regular expressions, but C++ does not. With Boost.Regex, you can search for a particular expression in a
string, replace parts of a string that match a regular
expression, and split a string into tokens
using regular expressions to define the delimiters. These techniques are
commonly used for text processing, parsing and input validation. We discuss the
Boost.Regex library in more detail in Section
21.7.


Result_of[9]



[9] Documentation for
Boost.Result_of, Doug Gregor, www.boost.org/libs/utility/utility.htm#result_of.


The class template result_of can
be used to specify the return type of a call expression—that is, an expression
that calls a function or calls the overloaded parentheses operator of a function
object. The call expression's return type is determined based on the types of
the arguments passed to the call expression. This can be helpful in templatizing
the return types of functions and function objects.


Smart_ptr[10]



[10] Documentation for
Boost.Smart_ptr, Greg Colvin and Beman Dawes,
www.boost.org/libs/smart_ptr/smart_ptr.htm.


Boost.Smart_ptr defines smart pointers that help you manage
dynamically allocated resources (e.g., memory, files and database connections).
Programmers often get confused about when to deallocate memory or simply forget
to do it, especially when the memory is referenced by more than one pointer.
Smart pointers take care of these tasks automatically. TR1 includes two smart
pointers from the Boost.Smart_ptr library. shared_ptrs handle lifetime
management of dynamically allocated objects. The memory is released when there
are no shared_ptrs referencing it. weak_ptrs
allow you to observe the value held by a shared_ptr without assuming
any management responsibilities. We discuss the Boost.Smart_ptr library
in more detail in Section
21.8.


Tuple[11]



[11] Documentation for
Boost.Tuple, Jaakko Järvi, www.boost.org/libs/tuple/doc/tuple_users_guide.html.


A tuple is a set of
objects. Boost.Tuple allows you to create sets of objects in a generic way and
allows generic functions to act on those sets. The library allows you to create
tuples of up to 10 objects; that limit can be extended. Boost.Tuple is
basically an extension to the STL's std::pair class template. Tuples are often used to return
multiple values from a function. They can also be used to store sets of elements
in an STL container where each set of elements is an element of the container.
Another useful feature is the ability to set the values of variables using the
elements of a tuple.


Type_traits[12]



[12] Documentation for
Boost.Type_traits, Steve Cleary, Beman Dawes,
Howard Hinnant and John Maddock, www.boost.org/doc/html/boost_typetraits.html.


Boost.Type_traits library helps abstract the differences
between types to allow generic programming implementations to be optimized. The
type_traits classes allow you to determine
specific traits of a type (e.g., is it a pointer or a reference type, or does
the type have a const qualifier?) and
perform type transformations to allow the object to be used in generic code.
Such information can be used to optimize generic code. For example, sometimes it
is more efficient to copy a collection of objects using the C function
memcpy rather than by iterating through
all the elements of the collection, as the STL copy
algorithm does. With the Boost.Type_traits
library, generic algorithms can be optimized by first checking the traits of the
types being processed, then performing the algorithm accordingly.


 


No comments:

Post a Comment