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<typename T>
void f(T&& param);

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

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

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

#include <iostream>
#include <type_traits>

template<typename T>
void f(T&& param)
{
	std::cout << std::boolalpha;
	std::cout << std::is_reference_v<T> << std::endl;
	//std::cout << std::is_reference_v<decltype(param)> << 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 <vector>

std::vector<int> vec;

template<typename T>
void f(T&& param)
{
	vec.push_back(std::forward<T>(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<class T>
void f(T t) {
  static_assert (std::is_arithmetic<T>::value); // Noncompliant
  using rawType = std::remove_cv<T>::type; // Noncompliant
}
template<class T>
void f(T t) {
  static_assert (std::is_arithmetic_v<T>); // Compliant, C++17
  using rawType = std::remove_cv_t<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<const int> << std::endl;
std::cout << std::is_const_v<std::remove_pointer_t<const int*>> << std::endl;
std::cout << std::is_const_v<std::remove_reference_t<const int&>> << std::endl;

输出全为 true。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值