std ::引用对 [英] std::pair of references

查看:130
本文介绍了std ::引用对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个 std :: pair 引用的有效性?特别是,有赋值运算符的问题吗?根据此链接,似乎没有使用operator =进行特殊处理,因此默认分配操作符

Is it valid to have a std::pair of references ? In particular, are there issues with the assignment operator ? According to this link, there seems to be no special treatment with operator=, so default assignement operator will not be able to be generated.

我想要一个对< T&&>

I'd like to have a pair<T&, U&> and be able to assign to it another pair (of values or references) and have the pointed-to objects modified.

推荐答案

请注意,我说可靠。一些常见的编译器仍在使用(对于GCC,我测试了GCC4.1, @Charles 报告GCC4.4.4)做不允许形成对引用的引用,但最近 允许它实现引用折叠( T& T if T 是引用类型)。如果你的代码使用这样的东西,你不能依赖它来工作在其他编译器,直到你尝试看看。

Notice that I said "reliably". Some common compilers still in use (for GCC, I tested GCC4.1, @Charles reported GCC4.4.4) do not allow forming a reference to a reference, but more recently do allow it as they implement reference collapsing (T& is T if T is a reference type). If your code uses such things, you cannot rely on it to work on other compilers until you try it and see.

听起来你想使用 boost :: tuple<>

int a, b;

// on the fly
boost::tie(a, b) = std::make_pair(1, 2);

// as variable
boost::tuple<int&, int&> t = boost::tie(a, b);
t.get<0>() = 1;
t.get<1>() = 2;

这篇关于std ::引用对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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