Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
168
5
return w;
}
编译器必须要么省略
w
的复制操作,要么让函数进行特别处理,以与下面这样的代码
等价:
Widget makeWidget()
{
Widget w;
return
std::move(w)
;
// w
会作为右值处理,
}
//
原因是复制省略没有实施
上述情况与按值传递的函数形参类似。它们作为函数返回值时,不适合实施复制省略,
但编译器必须在其返回时作为右值处理。以结果论,如果你的代码看起来是这样的:
Widget
makeWidget(
Widget w
)
//
按值传递的形参,
{
//
与函数返回值型别相同
return w
;
}
在编译器必须处理上面这段代码,以使它们与以下代码等价:
Widget makeWidget(Widget w)
{
return
std::move(w);
//
w
作为右值处理
}
这意味着,针对函数中按值返回的局部对象实施
std::move
的操作,不能给编译器
帮上忙(如果不执行复制省略,就必须将局部对象作为右值处理,效果一样),却
可以帮倒忙(可能会排除掉
RVO
的实施机会)。的确存在适合于针对局部变量实施
std::move
的情况(亦即,将其传递给某个函数,并且你确定自己不再会使用该变量),
但是其作为
return
语句的一部分时,要么适合
RVO
,要么返回一个按值形参,从而
并不属于以上的那些情况。
要点速记
针对右值引用的最后一次使用实施
std::move
,针对万能引用的最后一次使
用实施
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++程序设计:原理与实践(基础篇)(原书第2版)

C++程序设计:原理与实践(基础篇)(原书第2版)

本贾尼 斯特劳斯特鲁普
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749