C++ Technical Report 1
Technical Report 1 (TR1) is a draft document specifying additions to the C++ Standard Library such as regular expressions, smart pointers, hash tables, and random number generators. TR1 is not yet standardized, but likely will be part of the next official standard mostly as it stands now. In the meantime, vendors can use this document as a guide to create extensions. The report's goal is "to build more widespread existing practice for an expanded C++ standard library."[1]
Overview
Compilers need not include the TR1 components to be conforming, as TR1 is not yet officially part of the standard. Much of it is available from Boost, and several compiler/library distributors currently implement all or part of the components.
TR1 is not a complete list of additions to the library that will appear in the next standard; for example, the next standard, C++0x, may include support for threading. There is also a second technical report, TR2, planned for publishing after C++0x [2].
The new components are in the std::tr1
namespace to distinguish them from the current standard library.
Components
TR1 includes the following components:
General Utilities
Reference Wrapper
- lifted from Boost.Ref [3]
- additions to the
<functional>
header file -cref
,ref
,reference_wrapper
- enables passing references, rather than copies, into algorithms or function objects
Smart Pointers
- based on Boost Smart Pointer library [4]
- additions to the
<memory>
header file -shared_ptr
,weak_ptr
, etc - utility for memory management and exception safety using the Resource Acquisition Is Initialization (RAII) idiom
Function Objects
These four modules are added to the <functional>
header file:
Polymorphic Function Wrapper
function
- based on Boost.Function [5]
- stores all callables (function pointers, member function pointers, and function objects) that use a specified function call signature. The specific type of callable object is not required.
Function Object Binders
bind
- taken from Boost Bind library [6]
- generalized version of the standard
std::bind1st
andstd::bind2nd
- binds parameters to function objects, and allows for function composition.
Function Return Types
result_of
- taken from Boost
- determines the type of a call expression
mem_fn
mem_fn
- based on Boost Mem Fn library [7]
- enhancement to the standard
std::mem_fun
andstd::mem_fun_ref
- allows pointers to member functions to be treated as function objects
Metaprogramming and Type Traits
- new
<type_traits>
header file -is_pod
,has_virtual_destructor
,remove_extent
, etc - based on Boost Type Traits library [8]
- facilitates metaprogramming by enabling queries on and transformation between different types
Numerical Facilities
Random Number Generation
- new
<random>
header file -variate_generator
,mersenne twister
,poisson distribution
, etc - utilities for generating random numbers using various engines and probability distributions
Mathematical Functions
Some features of TR1, such as the mathematical functions and certain C99 additions, are not included in the Visual C++ implementation of TR1.
These functions will be of principal interest to programmers in the engineering and scientific disciplines.
The following table shows all 23 functions described in TR1.
Function name | Function prototype | Mathematical expression |
---|---|---|
Associated Laguerre polynomials | double assoc_laguerre( unsigned n, unsigned m, double x ) ; | |
Associated Legendre polynomials | double assoc_legendre( unsigned l, unsigned m, double x ) ; | |
Beta function | double beta( double x, double y ) ; | |
Complete elliptic integral of the first kind | double comp_ellint_1( double k ) ; | |
Complete elliptic integral of the second kind | double comp_ellint_2( double k ) ; | |
Complete elliptic integral of the third kind | double comp_ellint_3( double k , double nu ) ; | |
Confluent hypergeometric functions | double conf_hyperg( double a, double c, double x ) ; | |
Regular modified cylindrical Bessel functions | double cyl_bessel_i( double nu, double x ) ; | |
Cylindrical Bessel functions of the first kind | double cyl_bessel_j( double nu, double x ) ; | |
Irregular modified cylindrical Bessel functions | double cyl_bessel_k( double nu, double x ) ; | |
Cylindrical Neumann functions | double cyl_neumann( double nu, double x ) ; | |
Incomplete elliptic integral of the first kind | double ellint_1( double k, double phi ) ; | |
Incomplete elliptic integral of the second kind | double ellint_2( double k, double phi ) ; | |
Incomplete elliptic integral of the third kind | double ellint_3( double k, double nu, double phi ) ; | |
Exponential integral | double expint( double x ) ; | |
Hermite polynomials | double hermite( unsigned n, double x ) ; | |
Hypergeometric series | double hyperg( double a, double b, double c, double x ) ; | |
Laguerre polynomials | double laguerre( unsigned n, double x ) ; | |
Legendre polynomials | double legendre( unsigned l, double x ) ; | |
Riemann zeta function | double riemann_zeta( double x ) ; | |
Spherical Bessel functions of the first kind | double sph_bessel( unsigned n, double x ) ; | |
Spherical associated Legendre functions | double sph_legendre( unsigned l, unsigned m, double theta ) ; | |
Spherical Neumann functions | double sph_neumann( unsigned n, double x ) ; |
Each function has two additional variants. Appending the suffix ‘f’ or ‘l’ to a function name gives a function that operates on float
or long double
values respectively. For example:
float sph_neumannf( unsigned n, float x ) ;
long double sph_neumannl( unsigned n, long double x ) ;
Wrapper reference
A wrapper reference is obtained from an instance of the template class reference_wrapper
. Wrapper references are similar to normal references (‘&’) of the C++ language. To obtain a wrapper reference from any object the template class ref
is used (for a constant reference cref
is used).
Wrapper references are useful above all for template functions, when we need to obtain references to parameters rather than copies:
// This function will obtain a reference to the parameter 'r' and increase it.
void f( int &r ) { r++ ; }
// Template function.
template< class F, class P > void g( F f, P t ) { f(t) ; }
int main()
{
int i = 0 ;
g( f, i ) ; // 'g<void ( int &r ), int>' is instantiated
// then 'i' will not be modified.
cout << i << endl ; // Output -> 0
g( f, ref(i) ) ; // 'g<void(int &r),reference_wrapper<int>>' is instanced
// then 'i' will be modified.
cout << i << endl ; // Output -> 1
}
Containers
Tuple Types
- new
<tuple>
header file -tuple
- based on Boost Tuple library [9]
- vaguely an extension of the standard
std::pair
- fixed size collection of elements, which may be of different types
Fixed Size Array
- new
<array>
header file -array
- lifted from Boost Array library [10]
- as opposed to dynamic array types such as the standard
std::vector
Hash Tables
- new
<unordered_set>
,<unordered_map>
header files - new implementation, not derived from an existing library, not fully API compatible with existing libraries
- like all hash tables, often provide constant time lookup of elements but the worst case can be linear in the size of the container
Regular Expressions
- new
<regex>
header file -regex
,regex_match
,regex_search
,regex_replace
, etc - based on Boost RegEx library [11]
- pattern matching library
C Compatibility
C++ is designed to be compatible with the C programming language, but is not a strict superset of C due to diverging standards. TR1 attempts to reconcile some of these differences through additions to various headers in the C++ library, such as <complex>, <locale>, <cmath>, etc. These changes help to bring C++ more in line with the C99 version of the C standard (not all parts of C99 are included in TR1).
See also
- Boost library, a large collection of portable C++ libraries, several of which were included in TR1
- Standard Template Library, part of the current C++ Standard Library
- C++0x, the planned new standard for the C++ programming language
- Dinkumware, the only commercial vendor to fully implement TR1[citation needed]
Literature
- Peter Becker: "The C++ Standard Library Extensions: A Tutorial and Reference", 2006, ISBN 0-321-41299-0. Covers also the TR1 extensions.
External links
- http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf - pdf of the Proposed Draft
- http://aristeia.com/EC3E/TR1_info_frames.html - Scott Meyers' TR1 page, links to documents and articles