惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - BackSword

unity ui canvas shader texcoord.zw is not used for ui particle 问题记录,unity shaderlab 模版写入问题 textmeshpro 放大缩小出现黑色边框问题,修改shader FXAA 在桌面平台更高效的原因,MSAA在手机端更高效 glados优惠码 C# Array.Fill 值类型优化。 git 设置github代理 unity physics bug win10 ctrl+space 快捷键冲突问题 msvc C++编译链接 切线空间 c++局部静态变量是线程安全的 c++函数参数和返回值 状态同步 分享mkgmttime自实现功能。 关于socket通信中大小端转换问题 wpf clickonece 坑 [修复] 启动期间超频失败的错误信息 template return value error C2440: “初始化”: 无法从“const T”转换为“const Player *&”
c++返回值不能是右值对象
BackSword · 2022-12-30 · via 博客园 - BackSword

右值测试

结论

  • 返回值不能为右值对象,外面接的人,会那倒一个析构的对象使用。
  • 效率最高的,返回值对象通过引用传入,在函数里面赋值。

代码


#include <iostream>
#include<iostream>
#include<cstdlib>
#include <type_traits>

using namespace std;

template <class _Ty>
_NODISCARD constexpr _Ty&& forwardtt(
	remove_reference_t<_Ty>& _Arg) noexcept { // forward an lvalue as either an lvalue or an rvalue
	return static_cast<_Ty&&>(_Arg);
}

template <class _Ty>
_NODISCARD constexpr _Ty&& forwardtt(remove_reference_t<_Ty>&& _Arg) noexcept { // forward an rvalue as an rvalue
	static_assert(!is_lvalue_reference_v<_Ty>, "bad forward call");
	return static_cast<_Ty&&>(_Arg);
}


void processValue(int& a) { cout << "lvalue" << endl; }
void processValue(int&& a) { cout << "rvalue" << endl; }
template <typename T>
void forwardValue(T&& val)
{
	processValue(forwardtt<T>(val)); //照参数本来的类型进行转发。
}
template <typename T>
void nforwardValue(T&& val)
{
	processValue(val); //照参数本来的类型进行转发。
}
template <typename T>
void moveValue(T&& val)
{
	processValue(std::move(val)); //照参数本来的类型进行转发。
}
void Testdelcl()
{
	int i = 0;
	forwardValue(i); //传入左值 
	forwardValue(0);//传入右值 
	nforwardValue(i); //传入左值 
	nforwardValue(0);//传入右值 
	moveValue(i); //传入左值 
	moveValue(0);//传入右值 
}

using namespace std;

int g_constructCount = 0;
int g_copyConstructCount = 0;
int g_copyRConstructCount = 0;
int g_destructCount = 0;
struct A
{
	A() {
		cout << "construct: " << ++g_constructCount << " ptr:" << this << endl;
	}

	A(const A& a)
	{
		cout << "copy & construct: " << ++g_copyConstructCount << " ptr:" << this << " other:" << &a << endl;
	}

	A(A&& a)
	{
		cout << "copy && construct: " << ++g_copyRConstructCount << " ptr:" << this << " other:" << &a << endl;
	}

	~A()
	{
		cout << "destruct: " << ++g_destructCount << " ptr:" << this << endl;
	}

	void Print()
	{
		cout << "111" << " ptr:" << this << endl;
	}
};

A GetA()
{
	A a = A();

	return a;
}


A&& GetAA()
{
	return A();
}

void GetA(A& a)
{
}


class RTTPrint
{
	public:
		RTTPrint(int number)
		{
			m_number = number;
			cout<< "begin:" << m_number << endl;
		}
		~RTTPrint()
		{
			cout << "end  :"<< m_number << endl;
		}
		int m_number;
};

int main()
{
	int nCounter = 0;
	{
		Testdelcl();
	}
	{
		RTTPrint rtt(nCounter++);
		A a = GetA();
		cout << "haha " << endl;
		a.Print();
	}
	{
		RTTPrint rtt(nCounter++);
		A&& a = GetA();
		cout << "haha " << endl;
		a.Print();
	}
	{
		RTTPrint rtt(nCounter++);
		A b;
		GetA(b);
	}
	{
		RTTPrint rtt(nCounter++);
		A a = GetAA();
	}
	{
		RTTPrint rtt(nCounter++);
		A&& a = GetAA();
		cout << "haha " << endl;
		a.Print();
	}

	
	return 0;
}

输出结果

lvalue
rvalue
lvalue
lvalue
rvalue
rvalue
begin:0
construct: 1 ptr:0135FCEB
copy && construct: 1 ptr:0135FE53 other:0135FCEB
destruct: 1 ptr:0135FCEB
haha
111 ptr:0135FE53
destruct: 2 ptr:0135FE53
end  :0
begin:1
construct: 2 ptr:0135FCEB
copy && construct: 2 ptr:0135FE2F other:0135FCEB
destruct: 3 ptr:0135FCEB
haha
111 ptr:0135FE2F
destruct: 4 ptr:0135FE2F
end  :1
begin:2
construct: 3 ptr:0135FE17
destruct: 5 ptr:0135FE17
end  :2
begin:3
construct: 4 ptr:0135FC33
destruct: 6 ptr:0135FC33
copy && construct: 3 ptr:0135FDFF other:0135FC33
destruct: 7 ptr:0135FDFF
end  :3
begin:4
construct: 5 ptr:0135FC33
destruct: 8 ptr:0135FC33
haha
111 ptr:0135FC33
end  :4