C++ Technical Report 1
C++ Technical Report 1(TR1)是ISO/IEC TR 19768, C++ Library Extensions(函数库扩充)的一般名称。TR1是一份文档,内容提出了对C++标准函数库的追加项目。这些追加项目包括了正则表达式、智能指针、哈希表、随机数生成器等。TR1自己并非标准,它是一份草稿文档。然而它所提出的项目大多数已成为的C++11及之后版本的官方标准的一部分。这份文档的目标在于「为扩充的C++标准函数库创建更为广泛的现成实作品」。
概要
编译器并不需要保证包含TR1的组件,因为TR1并非官方标准的一部分。顺带一提,Boost提供了TR1大部分的实作,数个编译器/函数库开发商也已提供了各自的实作版本。
TR1并不代表下一届标准的全部;举例而言,下一届的标准C++11包含了线程的支持。
新的组件被放置在std::tr1
的命名空间(namespace)里,以和现在的标准函数库做区别。
TR1的内容
TR1包含以下组件:
一般用途
- 引用包装器(Reference Wrapper)
- 来自Boost.Ref [1]
- 在
<functional>
头文档中增加了 -cref
、ref
、reference_wrapper
- 可以对算法(algorithms)或仿函数(function objects)传递引用(references),而不是传递副本。
一个wrapper reference是由模板类reference_wrapper
产生的实体(instance)获得。wrapper reference近似于C++语言中的引用。
使用ref
以获得任何实例的wrapper reference(对常数引用const &使用cref
)。
wrapper reference对模板函数(template function)尤其有用,当模板参数推导不出引用的时候(范例如下:)
void f( int &r ) { r++; }
template< class Funct, class Arg >
void g( Funct f, Arg t )
{
f(t);
}
int main()
{
int i = 0;
g( f, i ); // 'g< void(int &r), int >' 被实例化
cout << i << endl; // 输出:0
g( f, ref(i) ); // 'g< void(int &r), reference_wrapper<int> >' 被实例化
cout << i << endl; // 输出:1
}
- 智能指针(Smart Pointers)
- 基于Boost Smart Pointer library[2]
- 由
<memory>
头文档增加了 -shared_ptr
、weak_ptr
等 - 将Resource Acquisition Is Initialization(RAII)手法用于内存管理和异常安全性。
仿函数
以下四个模块被加进<functional>
标头档之中:
- 多形态的函数包装器(Polymorphic Function Wrapper)
function
- 基于Boost.Function[3]
- 保存任何使用特定函数签名的"可调用物"(函数指针、成员函数指针、仿函数),不需要可调用物确切的类型。
- 仿函数绑定器(Function Object Binders)
bind
- 采纳自Boost Bind library[4]
- 标准
std::bind1st
和std::bind2nd
的通用版 - 将参数绑定给仿函数,并且允许函数的结合。
- 函数返回类型
result_of
- 采纳自Boost
- 决定函数调用的返回类型
- 成员函数
mem_fn
- 采纳自Boost Mem Fn library[5]
- 标准
std::mem_fun
和std::mem_fun_ref
的加强版 - 允许成员函数指针能够像仿函数一样
元编程和类型特性(Type Traits)
- 新的
<type_traits>
头文档 -is_pod
、has_virtual_destructor
、remove_extent
等 - 采纳自Boost Type Traits library[6]
- 允许类编查询以及类别间的转换,可促进元编程
随机数产生器
- 新的
<random>
头文档 -variate_generator
、mersenne_twister
、poisson_distribution
等 - 采纳自Boost Random Number Library[7]
数学函数
- 新的
<cmath>
/<math.h>
头文档 -beta
、legendre
等
- 23种数学函数
函数名 | 函数原型 | 数学表达式 |
---|---|---|
连带拉盖尔多项式 | double assoc_laguerre( unsigned n, unsigned m, double x ) ; | |
连带勒让德多项式 | double assoc_legendre( unsigned l, unsigned m, double x ) ; | |
Beta 函数 | double beta( double x, double y ) ; | |
第一类完全椭圆积分 | double comp_ellint_1( double k ) ; | |
第二类完全椭圆积分 | double comp_ellint_2( double k ) ; | |
第三类完全椭圆积分 | double comp_ellint_3( double k , double nu ) ; | |
合流超几何函数 | double conf_hyperg( double a, double c, double x ) ; | |
第一类变形贝塞尔函数 | double cyl_bessel_i( double nu, double x ) ; | |
第二类变形贝塞尔函数 | double cyl_bessel_j( double nu, double x ) ; | |
第三类变形贝塞尔函数 | double cyl_bessel_k( double nu, double x ) ; | |
柱诺依曼函数
第二类柱贝塞尔函数 |
double cyl_neumann( double nu, double x ) ; | |
第一类不完全椭圆积分 | double ellint_1( double k, double phi ) ; | |
第二类不完全椭圆积分 | double ellint_2( double k, double phi ) ; | |
第三类不完全椭圆积分 | double ellint_3( double k, double nu, double phi ) ; | |
指数积分 | double expint( double x ) ; | |
埃尔米特多项式 | double hermite( unsigned n, double x ) ; | |
超几何级数 | double hyperg( double a, double b, double c, double x ) ; | |
拉盖尔多项式 | double laguerre( unsigned n, double x ) ; | |
勒让德多项式 | double legendre( unsigned l, double x ) ; | |
黎曼zeta函数 | double riemann_zeta( double x ) ; | |
第一类球贝塞尔函数 | double sph_bessel( unsigned n, double x ) ; | |
球谐函数 | double sph_legendre( unsigned l, unsigned m, double theta ) ; | |
球诺依曼函数
第二类球贝塞尔函数 |
double sph_neumann( unsigned n, double x ) ; |
多元组类型(Tuple Types)
定量数组(Fixed Size Array)
- 新
<array>
标头档 -array
- 来自Boost Array library[9]
- 与动态数组类型,像是标准的
std::vector
相反,是静态的矩阵,但是能够享受类似于begin()等与std::vector
相似的接口。
哈希表(Hash Tables)
正则表达式
- 新
<regex>
标头档 -regex
、regex_match
、regex_search
、regex_replace
等 - 来自Boost RegEx library[10]
- pattern matching library
关联项目
- C++11,C++新标准
- C99,C语言标准
- Boost library,提供大量的C++程序库,数个包含于TR1
- STL标准模板库,现行C++标准程序库的一部分
参考文献
脚注
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03).
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03).
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03).
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03).
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03).
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-07-15).
- (页面存档备份,存于)
- . [2006-05-27]. (原始内容存档于2006-05-26).
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-07-01).
- . www.boost.org. [2022-07-01]. (原始内容存档于2022-07-11).
外部链接
- Scott Meyers' Effective C++: TR1 Information(页面存档备份,存于) - 包含TR1提案文档的链接,提供了TR1程序库的背景以及理由。
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.