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> 头文件中增加了 - crefrefreference_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)

仿函数[编辑]

以下四个模块被加进<functional>头文件之中:

多形态的函数包装器(Polymorphic Function Wrapper)
  • function
  • 基于Boost.Function[3]
  • 存储任何使用特定函数签名的"可调用物"(函数指针、成员函数指针、仿函数),不需要可调用物确切的类型。
仿函数绑定器(Function Object Binders)
  • bind
  • 采纳自Boost Bind library[4]
  • 标准std::bind1ststd::bind2nd的通用版
  • 将参数绑定给仿函数,并且允许函数的结合。
函数返回类型
  • result_of
  • 采纳自Boost
  • 决定函数调用的返回类型
成员函数
  • mem_fn
  • 采纳自Boost Mem Fn library[5]
  • 标准std::mem_funstd::mem_fun_ref的加强版
  • 允许成员函数指针能够像仿函数一样

元编程和类型特性(Type Traits)[编辑]

  • 新的<type_traits>头文件 - is_podhas_virtual_destructorremove_extent
  • 采纳自Boost Type Traits library[6]
  • 允许类编查询以及类别间的变换,可促进元编程

数值工具[编辑]

随机数产生器[编辑]

  • 新的<random>头文件 - variate_generatormersenne_twisterpoisson_distribution
  • 采纳自Boost Random Number Library[7]

数学函数[编辑]

  • 新的<cmath>/<math.h>头文件 - betalegendre
  • 23种数学函数
函数名 函数原型 数学表达式
连带拉盖尔多项式 double assoc_laguerre( unsigned n, unsigned m, double x ) ; <math>{L_n}^m(x) = (-1)^m \frac{d^m}{dx^m} L_{n+m}(x), \text{ for } x \ge 0</math>
连带勒让德多项式 double assoc_legendre( unsigned l, unsigned m, double x ) ; <math>{P_l}^m(x) = (1-x^2)^{m/2} \frac{d^m}{dx^m} P_l(x), \text{ for } x \ge 0</math>
Beta 函数 double beta( double x, double y ) ; <math>\Beta(x,y)=\frac{\Gamma(x) \Gamma(y)}{\Gamma(x+y)}</math>
第一类完全椭圆积分 double comp_ellint_1( double k ) ; <math>K(k) = F\left(k, \textstyle \frac{\pi}{2}\right) = \int_0^{\frac{\pi}{2}} \frac{d\theta}{\sqrt{1 - k^2 \sin^2 \theta}}</math>
第二类完全椭圆积分 double comp_ellint_2( double k ) ; <math>E\left(k, \textstyle \frac{\pi}{2}\right) = \int_0^{\frac{\pi}{2}} \sqrt{1 - k^2 \sin^2 \theta}\; d\theta</math>
第三类完全椭圆积分 double comp_ellint_3( double k , double nu ) ; <math>\Pi\left(\nu, k, \textstyle \frac{\pi}{2}\right) = \int_0^{\frac{\pi}{2}} \frac{d\theta}{(1 - \nu \sin^2 \theta)\sqrt{1 - k^2 \sin^2 \theta}}</math>
合流超几何函数 double conf_hyperg( double a, double c, double x ) ; <math>F(a, c, x) = \frac{\Gamma(c)}{\Gamma(a)} \sum_{n = 0}^\infty \frac{\Gamma(a + n) x^n}{\Gamma(c + n) n!}</math>
第一类变形贝塞尔函数 double cyl_bessel_i( double nu, double x ) ; <math>I_\nu(x) = i^{-\nu} J_\nu(ix) = \sum_{k = 0}^\infty \frac{(x/2)^{\nu + 2k}}{k! \; \Gamma(\nu + k + 1)}, \text{ for } x \ge 0</math>
第二类变形贝塞尔函数 double cyl_bessel_j( double nu, double x ) ; <math>J_\nu(x) = \sum_{k = 0}^\infty \frac{(-1)^k \; (x/2)^{\nu + 2k}}{k! \; \Gamma(\nu + k + 1)}, \text{ for } x \ge 0</math>
第三类变形贝塞尔函数 double cyl_bessel_k( double nu, double x ) ; <math>\begin{align}

K_\nu(x) & = \textstyle\frac{\pi}{2} i^{\nu+1} \big(J_\nu(ix) + i N_\nu(ix)\big) \\

        & = \begin{cases}
                \displaystyle \frac{I_{-\nu}(x) - I_\nu(x)}{\sin \nu\pi}, & \text{for } x \ge 0 \text{ and } \nu \notin \mathbb{Z} \\[10pt]
                \displaystyle \frac{\pi}{2} \lim_{\mu \to \nu} \frac{I_{-\mu}(x) - I_\mu(x)}{\sin \mu\pi}, & \text{for } x < 0 \text{ and } \nu \in \mathbb{Z} \\
            \end{cases}

\end{align}</math>

柱诺依曼函数

第二类柱贝塞尔函数

double cyl_neumann( double nu, double x ) ; <math>

N_\nu(x) = \begin{cases}

                \displaystyle \frac{J_\nu(x)\cos \nu\pi - J_{-\nu}(x)}{\sin \nu\pi}, & \text{for } x \ge 0 \text{ and } \nu \notin \mathbb{Z} \\[10pt]
                \displaystyle \lim_{\mu \to \nu} \frac{J_\mu(x)\cos \mu\pi - J_{-\mu}(x)}{\sin \mu\pi}, & \text{for } x < 0 \text{ and } \nu \in \mathbb{Z} \\
            \end{cases}

</math>

第一类不完全椭圆积分 double ellint_1( double k, double phi ) ; k\right| \le 1</math>
第二类不完全椭圆积分 double ellint_2( double k, double phi ) ; k\right| \le 1</math>
第三类不完全椭圆积分 double ellint_3( double k, double nu, double phi ) ; k\right| \le 1</math>
指数积分 double expint( double x ) ; <math> \mbox{E}i(x)=-\int_{-x}^{\infty} \frac{e^{-t}}{t}\, dt</math>
埃尔米特多项式 double hermite( unsigned n, double x ) ; <math>H_n(x)=(-1)^n e^{x^2}\frac{d^n}{dx^n}e^{-x^2}\,\!</math>
超几何级数 double hyperg( double a, double b, double c, double x ) ; <math>F(a,b,c,x)=\frac{\Gamma(c)}{\Gamma(a)\Gamma(b)}\sum_{n = 0}^\infty\frac{\Gamma(a+n)\Gamma(b+n)}{\Gamma(c+n)}\frac{x^n}{n!}</math>
拉盖尔多项式 double laguerre( unsigned n, double x ) ; <math>L_n(x)=\frac{e^x}{n!}\frac{d^n}{dx^n}\left(x^n e^{-x}\right), \text{ for } x \ge 0</math>
勒让德多项式 double legendre( unsigned l, double x ) ; x\right| \le 1 </math>
黎曼zeta函数 double riemann_zeta( double x ) ; <math>

\Zeta(x) =

         \begin{cases}
                \displaystyle \sum_{k = 1}^\infty k^{-x}, & \text{for } x > 1 \\[10pt]
                \displaystyle 2^x\pi^{x-1}\sin\left(\frac{x\pi}{2}\right)\Gamma(1-x)\zeta(1-x), & \text{for } x < 1 \\
            \end{cases}

</math>

第一类球贝塞尔函数 double sph_bessel( unsigned n, double x ) ; <math>j_n(x) = \sqrt{\frac{\pi}{2x}} J_{n+1/2}(x), \text{ for } x \ge 0</math>
球谐函数 double sph_legendre( unsigned l, unsigned m, double theta ) ; m| \leq l</math>
球诺依曼函数

第二类球贝塞尔函数

double sph_neumann( unsigned n, double x ) ; <math>n_n(x) = \left(\frac{\pi}{2x}\right)^{\frac{1}{2}}N_{n+\frac{1}{2}}(x), \text{ for } x \ge 0</math>

容器[编辑]

多元组类型(Tuple Types)[编辑]

  • <tuple>头文件 - tuple
  • 采纳自Boost Tuple library[8]
  • 标准std::pair的扩展
  • 固定尺寸的元素集合,元素可以是不同的类型

定量数组(Fixed Size Array)[编辑]

  • <array>头文件 - array
  • 来自Boost Array library[9]
  • 与动态数组类型,像是标准的std::vector相反,是静态的矩阵,但是能够享受类似于begin()等与std::vector相似的接口。

哈希表(Hash Tables)[编辑]

  • <unordered_set><unordered_map>头文件
  • 完全是新的实现,不派生自既有之程序库。与既有之程序库API并不完全兼容
  • 就如同所有的哈希表提供常数时间的元素查找,但最坏情况查找时间与容器的大小呈线性关系。

正则表达式[编辑]

  • <regex>头文件 - regexregex_matchregex_searchregex_replace
  • 来自Boost RegEx library[10]
  • pattern matching library

C的兼容性[编辑]

C++被设计成与C语言兼容,但由于不同的标准,C++并不是C的严格超集合。TR1试图调和这些差异,透过对各种头文件,如<complex>、<locale>、<cmath>等进行扩展。 这些改变帮助C++能够与C99版本的C标准更为一致(并非所有C99都包含于TR1)。

关系项目[编辑]

参考文献[编辑]

脚注[编辑]

  1. ^ ref - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  2. ^ Boost.SmartPtr: The Smart Pointer Library - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  3. ^ Chapter 16. Boost.Function - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  4. ^ Chapter 1. Boost.Bind - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  5. ^ Chapter 1. Boost.Member Function - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-04-03). 
  6. ^ Chapter 1. Boost.TypeTraits - 1.37.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-07-15). 
  7. ^ [1]页面存档备份,存于互联网档案馆
  8. ^ The Boost Tuple Library – Boost 1.48.0. [2006-05-27]. (原始内容存档于2006-05-26). 
  9. ^ Chapter 5. Boost.Array - 1.72.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-07-01). 
  10. ^ Boost.Regex - 1.36.0. www.boost.org. [2022-07-01]. (原始内容存档于2022-07-11). 

其他[编辑]

外部链接[编辑]