C ++ std :: ref(T)与T&amp ;?之间的区别 [英] C++ Difference between std::ref(T) and T&?

查看:69
本文介绍了C ++ std :: ref(T)与T&amp ;?之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此程序有一些疑问:

I have some questions regarding this program:

#include <iostream>
#include <type_traits>
#include <functional>
using namespace std;
template <typename T> void foo ( T x )
{
    auto r=ref(x);
    cout<<boolalpha;
    cout<<is_same<T&,decltype(r)>::value;
}
int main()
{
    int x=5;
    foo (x);
    return 0;
}

输出为:

false

我想知道,如果 std :: ref 不返回对象的引用,那么它有什么作用?基本上,两者之间有什么区别?

I want to know, if std::ref doesn't return the reference of an object, then what does it do? Basically, what is the difference between:

T x;
auto r = ref(x);

T x;
T &y = x;

我还想知道为什么存在这种差异?为什么我们有引用(即 T& )时需要 std :: ref std :: reference_wrapper ?

Also, I want to know why does this difference exist? Why do we need std::ref or std::reference_wrapper when we have references (i.e. T&)?

推荐答案

ref 构造一个具有适当 reference_wrapper 类型的对象,以保存对对象的引用.这意味着您申请的时间:

Well ref constructs an object of the appropriate reference_wrapper type to hold a reference to an object. Which means when you apply:

auto r = ref(x);

这将返回一个 reference_wrapper ,而不是对 x 的直接引用(即 T& ).该 reference_wrapper (即 r )保存了 T& .

This returns a reference_wrapper and not a direct reference to x (ie T&). This reference_wrapper (ie r) instead holds T&.

当您要模拟可复制对象的 reference 时, reference_wrapper 非常有用(它们都是 copy-constructible copy-assignable ).

A reference_wrapper is very useful when you want to emulate a reference of an object which can be copied (it is both copy-constructible and copy-assignable).

在C ++中,一旦创建了对对象(例如 x )的引用(例如 y ),然后创建了 y x 共享相同的基地址.此外, y 不能引用任何其他对象.另外,您不能创建引用数组,即,这样的代码将引发错误:

In C++, once you create a reference (say y) to an object (say x), then y and x share the same base address. Furthermore, y cannot refer to any other object. Also you cannot create an array of references ie code like this will throw an error:

#include <iostream>
using namespace std;

int main()
{
    int x=5, y=7, z=8;
    int& arr[] {x,y,z};    // error: declaration of 'arr' as array of references
    return 0;
}

但这是合法的:

#include <iostream>
#include <functional>  // for reference_wrapper
using namespace std;

int main()
{
    int x=5, y=7, z=8;
    reference_wrapper<int> arr[] {x,y,z};
    for (auto a: arr)
        cout << a << " ";
    return 0;
}
/* OUTPUT:
5 7 8
*/

谈论您使用 cout<<is_same< T&,decltype(r)> :: value; ,解决方案是:

Talking about your problem with cout << is_same<T&,decltype(r)>::value;, the solution is:

cout << is_same<T&,decltype(r.get())>::value;  // will yield true

让我为您展示程序:

#include <iostream>
#include <type_traits>
#include <functional>
using namespace std;

int main()
{
    cout << boolalpha;
    int x=5, y=7;
    reference_wrapper<int> r=x;   // or auto r = ref(x);
    cout << is_same<int&, decltype(r.get())>::value << "\n";
    cout << (&x==&r.get()) << "\n";
    r=y;
    cout << (&y==&r.get()) << "\n";
    r.get()=70;
    cout << y;
    return 0;
}
/* Ouput:
true
true
true
70
*/

在这里,我们了解三件事:

See here we get to know three things:

  1. 一个 reference_wrapper 对象(此处为 r )可用于创建一个引用数组,而是不可能的> T& .

  1. A reference_wrapper object (here r) can be used to create an array of references which was not possible with T&.

r 实际上就像一个真实的引用(请参阅 r.get()= 70 如何更改 y 的值).

r actually acts like a real reference (see how r.get()=70 changed the value of y).

r T& 不同,但 r.get()是.这意味着 r 持有 T& ,即,顾名思义,它是围绕引用的包装器 T& .

r is not same as T& but r.get() is. This means that r holds T& ie as its name suggests is a wrapper around a reference T&.

我希望这个答案足以解释您的疑问.

I hope this answer is more than enough to explain your doubts.

这篇关于C ++ std :: ref(T)与T&amp ;?之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆