C ++中的reference_wrapper
在C++中,reference_wrapper是一个类模板,可通过将引用包装在类型为T的可复制构造和可复制分配对象中来提供帮助。std::reference_wrapper的实例基本上是对象,但可以将它们转换为T&。因此,我们可以将通过引用使用基础类型的函数用作参数。
范例程式码
#include <iostream>
#include <functional>
using namespace std;
int main () {
char a = 'h', b = 'e', c = 'l', d = 'l', e = 'o' , f = 'W', g = 'o', h = 'r', i = 'l', j = 'd';
reference_wrapper<char> ref[] = {a, b, c, d, e, f, g, h, i, j}; //creating reference array
for (char& s : ref)
cout << s;
cout <<endl;
return 0;
}输出结果
soumyadeep@soumyadeep-VirtualBox:~$ ./a.out helloWorld soumyadeep@soumyadeep-VirtualBox:~$