栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

move 和 forward

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

move 和 forward

move

关键的话:

std::move 并不进行任何移动,std::forward 也不进行任何转发。

move 和 forward 在运行期都无所作为。不会生成任何可执行代码,连一个字节都不会生成。

函数形参永远是左值。 具名变量永远是左值。但是不具名的变量不一定是右值。(例如字符串字面量 “hello world” 也是左值)

参考:
https://zh.cppreference.com/w/cpp/language/identifiers

命名某个变量、函数、概念的特化 (C++20 起)或枚举项的标识符可以作为表达式使用。仅由这个标识符组成的表达式的结果,是该标识符所命名的实体。若该标识符命名的是某个函数、变量、模板形参对象 (C++20 起)或数据成员,则表达式的值类别为左值,否则为纯右值(例如枚举项是纯右值表达式,概念的特化是 bool 纯右值 (C++20 起))。

可以看到,一个枚举项命名的并非是某个函数、变量、模板形参对象 (C++20 起)或数据成员,因此非左值,为纯右值。

我理解中左值就是可以出现在operator=左侧,而右值是只能出现在operator=右侧。
但是对于C++自定义的类,会有一些意外。比如string()是一个临时对象但也能放在左边,不过没有意义。

值类别参考:https://zh.cppreference.com/w/cpp/language/value_category

再参考标准规格书:ISO_IEC_14882__2020-12

In general, the effect of this rule is that named rvalue references are treated as lvalues and unnamed rvalue references to objects are treated as xvalues; rvalue references to functions are treated as lvalues whether named or not.

具名右值引用是一个左值,不具名右值引用是一个亡值 (xvalue)(“将亡 (expiring)”的值)

forward

forward 不进行任何转发。只是在某个特定条件满足下执行一个强制转换(static_cast)罢了。

首先理解万能引用的模板推导规则:

template
void f(T&& param);

万能引用只能是 T&& 的形式,即 T 不能被修饰(如 cv 修饰符,否则为右值引用形式)。

推导规则:左值 则 T 被推导为 左值引用,右值 则 T 被推导为 不带引用的情况。

例如(一个简单的测试万能引用的程序):

#include 
#include 

template
void f(T&& param)
{
	std::cout << std::boolalpha;
	std::cout << std::is_reference_v << std::endl;
	//std::cout << std::is_reference_v << std::endl;
}

int main()
{
	const int a = 5;
	f(a); // T 为 const int& 
	f(10);
}

推导不会丢掉 cv 修饰符 (cv (const and volatile) type qualifiers)。

boolalpha操纵符,否则 ture 会输出为 1

引用折叠:&& + && == &&

只有四种语境会发生:模板实例化、auto型别生成、创建和运用 typedef 和别名声明、decltype

个人观点:其实就是推导(deduction)语境。

简单示例:

#include 

std::vector vec;

template
void f(T&& param)
{
	vec.push_back(std::forward(param));
}

int main()
{
	int b = 5;
	const int& a = b;
	f(5);
	f(a);
}
关于 _t 与 _v

参考:https://rules.sonarsource.com/cpp/RSPEC-6020

The “_t” and “_v” version of type traits should be used instead of “::type” and “::value”

Even if the old variant still exists, the new one, which uses _t (C++14) and _v (C++17) suffixes as discriminant, should be preferred.

template
void f(T t) {
  static_assert (std::is_arithmetic::value); // Noncompliant
  using rawType = std::remove_cv::type; // Noncompliant
}
template
void f(T t) {
  static_assert (std::is_arithmetic_v); // Compliant, C++17
  using rawType = std::remove_cv_t; // Compliant, C++14
}
top-level const qualifier

C++Primer 5th 2.4.3 :

可见,常量指针为顶层const,指向常量的指针为底层const,const int* const 既是顶层又是底层。引用则没有顶层。

而 is_const_v 只能检查顶层const。要检查底层const,如下:

std::cout << std::boolalpha;
std::cout << std::is_const_v << std::endl;
std::cout << std::is_const_v> << std::endl;
std::cout << std::is_const_v> << std::endl;

输出全为 true。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/717140.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号