C++中的参考文献包装器

标准:参考包装 是一个类模板,它将一个引用封装在一个可复制可构造且可复制可赋值的对象或对T类型函数的引用中。 实例 标准:参考包装 是对象(可以复制或存储在容器中),但可以隐式转换为 “T&” 这样它们就可以作为参数,用于引用底层类型的函数。

null

语法:

template <class T> class reference_wrapper;
template parameter(T): type of the referred element and
                        this can be  either function or object.

例子:

// C++ program to demonstrate the
// use of std::reference_wrapper
#include <iostream>
#include <functional>
using namespace std;
int main ()
{
char a = 'g' , b = 'e' , c  = 'e' , d = 'k' , e = 's' ;
// creating an array of character "references":
reference_wrapper< char > ref[] = {a, b, c, d, e};
for ( char & s : ref)
cout << s;
return 0;
}


输出:

geeks
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享