关于c++右值和常引用
  • 板块学术版
  • 楼主蒟酱厂妹
  • 当前回复9
  • 已保存回复9
  • 发布时间2022/4/12 14:48
  • 上次更新2023/10/28 03:54:58
查看原帖
关于c++右值和常引用
310818
蒟酱厂妹楼主2022/4/12 14:48
#include<iostream>
#include<cstdio>
#include<algorithm>
using std::cin;using std::cout;
inline int re1(){return 1;}
inline int re2(){return 2;}
inline int re3(){return 3;}
inline int sum(const int &x,const int &y){return x+y;}
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	std::ios::sync_with_stdio(false);cin.tie(nullptr);
	int &&n4=re1()+re1()+re2();
	cout<<sum(n4,re2()+re3());
	return 0;
}

这段代码输出正确,但是 re2()+re3() 是个右值,n4 也是个右值,但是 sum 函数的参数是常引用,也就是说右值可以传给常引用??

2022/4/12 14:48
加载中...