服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++98/11/17表达式类别(小结)

C++98/11/17表达式类别(小结)

2021-09-07 15:32jerry_fuyi C/C++

这篇文章主要介绍了C++98/11/17表达式类别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

目标

以下代码能否编译通过,能否按照期望运行?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <utility>
#include <type_traits>
 
namespace cpp98
{
 
struct A { };
A func() { return A(); }
 
int main()
{
 int i = 1;
 i = 2;
 // 3 = 4;
 const int j = 5;
 // j = 6;
 i = j;
 func() = A();
 return 0;
}
 
}
 
namespace cpp11
{
 
#define is_lvalue(x) std::is_lvalue_reference<decltype((x))>::value
#define is_prvalue(x) !std::is_reference<decltype((x))>::value
#define is_xvalue(x) std::is_rvalue_reference<decltype((x))>::value
#define is_glvalue(x) (is_lvalue(x) || is_xvalue(x))
#define is_rvalue(x) (is_xvalue(x) || is_prvalue(x))
 
void func();
int non_reference();
int&& rvalue_reference();
std::pair<int, int> make();
 
struct Test
{
 int field;
 void member_function()
 {
 static_assert(is_lvalue(field), "");
 static_assert(is_prvalue(this), "");
 }
 enum Enum
 {
 ENUMERATOR,
 };
};
 
int main()
{
 int i;
 int&& j = std::move(i);
 Test test;
 
 static_assert(is_lvalue(i), "");
 static_assert(is_lvalue(j), "");
 static_assert(std::is_rvalue_reference<decltype(j)>::value, "");
 static_assert(is_lvalue(func), "");
 static_assert(is_lvalue(test.field), "");
 static_assert(is_lvalue("hello"), "");
 
 static_assert(is_prvalue(2), "");
 static_assert(is_prvalue(non_reference()), "");
 static_assert(is_prvalue(Test{3}), "");
 static_assert(is_prvalue(test.ENUMERATOR), "");
 
 static_assert(is_xvalue(rvalue_reference()), "");
 static_assert(is_xvalue(make().first), "");
 
 return 0;
}
 
}
 
namespace reference
{
 
int&& rvalue_reference()
{
 int local = 1;
 return std::move(local);
}
 
const int& const_lvalue_reference(const int& arg)
{
 return arg;
}
 
int main()
{
 auto&& i = rvalue_reference(); // dangling reference
 auto&& j = const_lvalue_reference(2); // dangling reference
 int k = 3;
 auto&& l = const_lvalue_reference(k);
 return 0;
}
 
}
 
namespace auto_decl
{
 
int non_reference() { return 1; }
int& lvalue_reference() { static int i; return i; }
const int& const_lvalue_reference() { return lvalue_reference(); }
int&& rvalue_reference() { static int i; return std::move(i); }
 
int main()
{
 auto [s1, s2] = std::pair(2, 3);
 auto&& t1 = s1;
 static_assert(!std::is_reference<decltype(s1)>::value);
 static_assert(std::is_lvalue_reference<decltype(t1)>::value);
 
 int i1 = 4;
 auto i2 = i1;
 decltype(auto) i3 = i1;
 decltype(auto) i4{i1};
 decltype(auto) i5 = (i1);
 static_assert(!std::is_reference<decltype(i2)>::value, "");
 static_assert(!std::is_reference<decltype(i3)>::value, "");
 static_assert(!std::is_reference<decltype(i4)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(i5)>::value, "");
 
 auto n1 = non_reference();
 decltype(auto) n2 = non_reference();
 auto&& n3 = non_reference();
 static_assert(!std::is_reference<decltype(n1)>::value, "");
 static_assert(!std::is_reference<decltype(n2)>::value, "");
 static_assert(std::is_rvalue_reference<decltype(n3)>::value, "");
 
 auto l1 = lvalue_reference();
 decltype(auto) l2 = lvalue_reference();
 auto&& l3 = lvalue_reference();
 static_assert(!std::is_reference<decltype(l1)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(l2)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(l3)>::value, "");
 
 auto c1 = const_lvalue_reference();
 decltype(auto) c2 = const_lvalue_reference();
 auto&& c3 = const_lvalue_reference();
 static_assert(!std::is_reference<decltype(c1)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(c2)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(c3)>::value, "");
 
 auto r1 = rvalue_reference();
 decltype(auto) r2 = rvalue_reference();
 auto&& r3 = rvalue_reference();
 static_assert(!std::is_reference<decltype(r1)>::value, "");
 static_assert(std::is_rvalue_reference<decltype(r2)>::value, "");
 static_assert(std::is_rvalue_reference<decltype(r3)>::value, "");
 
 return 0;
}
 
}
 
namespace cpp17
{
 
class NonMoveable
{
public:
 int i = 1;
 NonMoveable(int i) : i(i) { }
 NonMoveable(NonMoveable&&) = delete;
};
 
NonMoveable make(int i)
{
 return NonMoveable{i};
}
 
void take(NonMoveable nm)
{
 return static_cast<void>(nm);
}
 
int main()
{
 auto nm = make(2);
 auto nm2 = NonMoveable{make(3)};
 // take(nm);
 take(make(4));
 take(NonMoveable{make(5)});
 return 0;
}
 
}
 
int main()
{
 cpp98::main();
 cpp11::main();
 reference::main();
 auto_decl::main();
 cpp17::main();
}

C++98表达式类别

每个C++表达式都有一个类型:42的类型为int,int i;则(i)的类型为int&。这些类型落入若干类别中。在C++98/03中,每个表达式都是左值或右值。

左值(lvalue)是指向真实储存在内存或寄存器中的值的表达式。“l”指的是“left-hand side”,因为在C中只有lvalue才能写在赋值运算符的左边。相对地,右值(rvalue,“r”指的是“right-hand side”)只能出现在赋值运算符的右边。

有一些例外,如const int i;,i虽然是左值但不能出现在赋值运算符的左边。到了C++,类类型的rvalue却可以出现在赋值运算符的左边,事实上这里的赋值是对赋值运算符函数的调用,与基本类型的赋值是不同的。

lvalue可以理解为可取地址的值,变量、对指针解引用、对返回类型为引用类型的函数的调用等,都是lvalue。临时对象都是rvalue,包括字面量和返回类型为非引用类型的函数调用等。字符串字面量是个例外,它属于不可修改的左值。

赋值运算符左边需要一个lvalue,右边需要一个rvalue,如果给它一个lvalue,该lvalue会被隐式转换成rvalue。这个过程是理所当然的。

动机

C++11引入了右值引用和移动语义。函数返回的右值引用,顾名思义,应该表现得和右值一样,但是这会破坏很多既有的规则:

  • rvalue是匿名的,不一定有存储空间,但右值引用指向内存中的具体对象,该对象还要被维护着;
  • rvalue的类型是确定的,必须是完全类型,静态类型与动态类型相同,而右值引用可以是不完全类型,也可以支持多态;
  • 非类类型的rvalue没有cv修饰(const和volatile),但右值引用可以有,而且修饰符必须保留。

这给传统的lvalue/rvalue二分法带来了挑战,C++委员会面临选择:

  • 维持右值引用是rvalue,添加一些特殊规则;
  • 把右值引用归为lvalue,添加一些特殊规则;
  • 细化表达式类别。

上述问题只是冰山一角;历史选择了第三种方案。

C++11表达式类别

C++11提出了表达式类别(value category)的概念。虽然名叫“value category”,但类别划分的是表达式而不是值,所以我从标题开始就把它译为“表达式类别”。C++标准定义表达式为:

An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects.

每个表达式都是三种类别之一:左值(lvalue)、消亡值(xvalue)和纯右值(prvalue),称为主类别。还有两种混合类别:lvalue和xvalue统称范左值(glvalue),xvalue和prvalue统称右值(rvalue)。

C++98/11/17表达式类别(小结)

?
1
2
#define is_glvalue(x) (is_lvalue(x) || is_xvalue(x))
#define is_rvalue(x) (is_xvalue(x) || is_prvalue(x))

C++11对这些类别的定义如下:

  • lvalue指定一个函数或一个对象;
  • xvalue(eXpiring vavlue)也指向对象,通常接近其生命周期的终点;一些涉及右值引用的表达式的结果是xvalue;
  • gvalue(generalized lvalue)是一个lvalue或xvalue;
  • rvalue是xvalue、临时对象或它们的子对象,或者没有关联对象的值;
  • prvalue(pure rvalue)是不是xvalue的rvalue。

这种定义不是很清晰。具体来讲,lvalue包括:(点击展开)

lvalue的性质:

  • 与glvalue相同;
  • 内置取地址运算符可以作用于lvalue;
  • 可修改的lvalue可以出现在内置赋值运算符的左边;
  • 可以用来初始化一个左值引用。

prvalue包括:

prvalue的性质:

  • 与rvalue相同;
  • 不能是多态的;
  • 非类类型且非数组的prvalue没有cv修饰符,即使写了也没有;
  • 必须是完全类型;
  • 不能是抽象类型或其数组。

xvalue包括:

xvalue的性质;

  • 与rvalue相同;
  • 与glvalue相同。

glvalue的性质:

  • 可以隐式转换为prvalue;
  • 可以是多态的;
  • 可以是不完全类型。

rvalue的性质:

  • 内置取地址运算符不能作用于rvalue;
  • 不能出现在内置赋值或复合赋值运算符的左边;
  • 可以绑定给const左值引用(见下);
  • 可以用来初始化右值引用(见下);
  • 如果一个函数有右值引用参数和const左值引用参数两个重载,传入一个rvalue时,右值引用的那个重载被调用。

还有一些特殊的分类:

  • 对于非静态成员函数mf及其指针pmf,a.mf、p->mf、a.*pmf和p->*pmf都被归类为prvalue,但它们不是常规的prvalue,而是pending(即将发生的) member function call,只能用于函数调用;
  • 返回void的函数调用、向void的类型装换和throw语句都是void表达式,不能用于初始化引用或函数参数;
  • C++中最小的寻址单位是字节,因此位域不能绑定到非const左值引用上;const左值引用和右值引用可以绑定位域,它们指向的是位域的一个拷贝。

终于把5个类别介绍完了。表达式可以分为lvalue、xvalue和prvalue三类,lvalue和prvalue与C++98中的lvalue和rvalue类似,而xvalue则完全是为右值引用而生,兼有glvalue与rvalue的性质。除了这种三分类法外,表达式还可以分为lvalue和rvalue两类,它们之间的主要差别在于是否可以取地址;还可以分为glvalue和prvalue两类,它们之间的主要差别在于是否存在实体,glvalue有实体,因而可以修改原对象,xvalue常被压榨剩余价值。

引用绑定

我们稍微岔开一会,来看两个与表达式分类相关的特性。

引用绑定有以下类型:

  • 左值引用绑定lvalue,cv修饰符只能多不能少;
  • 右值引用可以绑定rvalue,我们通常不给右值引用加cv修饰符;
  • const左值引用可以绑定rvalue。

左值引用绑定lvalue天经地义,没什么需要关照的。但rvalue都是临时对象,绑定给引用就意味着要继续用它,它的生命周期会受到影响。通常,rvalue的生命周期会延长到绑定引用的声明周期,但有以下例外:

  • 由return语句返回的临时对象在return语句结束后即销毁,这样的函数总是会返回一个空悬引用(dangling reference);
  • 绑定到初始化列表中的引用的临时对象的生命周期只延长到构造函数结束——这是个缺陷,在C++14中被修复;
  • 绑定到函数参数的临时对象的生命周期延长到函数调用所在表达式结束,把该参数作为引用返回会得到空悬引用;
  • 绑定到new表达式中的引用的临时对象的生命周期只延长到包含new的表达式的结束,不会跟着那个对象。

简而言之,临时变量的生命周期只能延长一次。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <utility>
 
int&& rvalue_reference()
{
 int local = 1;
 return std::move(local);
}
 
const int& const_lvalue_reference(const int& arg)
{
 return arg;
}
 
int main()
{
 auto&& i = rvalue_reference(); // dangling reference
 auto&& j = const_lvalue_reference(2); // dangling reference
 int k = 3;
 auto&& l = const_lvalue_reference(k);
}

rvalue_reference返回一个指向局部变量的引用,因此i是空悬引用;2绑定到const_lvalue_reference的参数arg上,函数返回后延长的生命周期达到终点,因此j也是悬空引用;k在传参的过程中根本没有临时对象创建出来,所以l不是空悬引用,它是指向k的const左值引用。

auto与decltype

从C++11开始,auto关键字用于自动推导类型,用的是模板参数推导的规则:如果是拷贝列表初始化,则对应模板参数为std::initializer_list<T>,否则把auto替换为T。至于详细的模板参数推导规则,要介绍的话未免喧宾夺主了。

还好,这不是我们的重点。在引出重点之前,我们还得先看decltype。

decltype用于声明一个类型("declare type"),有两种语法:

  • decltype(entity);
  • decltype(expression)。

第一种,decltype的参数是没有括号包裹的标识符或类成员,则decltype产生该实体的类型;如果是结构化绑定,则产生被引类型。

第二种,decltype的参数是不能匹配第一种的任何表达式,其类型为T,则根据其表达式类别讨论:

  • 如果是xvalue,产生T&&——#define is_xvalue(x) std::is_rvalue_reference<decltype((x))>::value;
  • 如果是lvalue,产生T&——#define is_lvalue(x) std::is_lvalue_reference<decltype((x))>::value;
  • 如果是prvalue,产生T——#define is_prvalue(x) !std::is_reference<decltype((x))>::value。

因此,decltype(x)和decltype((x))产生的类型通常是不同的。

对于不带引用修饰的auto,初始化器的表达式类别会被抹去,为此C++14引入了新语法decltype(auto),产生的类型为decltype(expr),其中expr为初始化器。对于局部变量,等号右边加上一对圆括号,可以保留表达式类别。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <utility>
#include <type_traits>
 
int non_reference() { return 1; }
int& lvalue_reference() { static int i; return i; }
const int& const_lvalue_reference() { return lvalue_reference(); }
int&& rvalue_reference() { static int i; return std::move(i); }
 
int main()
{
 auto [s1, s2] = std::pair(2, 3);
 auto&& t1 = s1;
 static_assert(!std::is_reference<decltype(s1)>::value);
 static_assert(std::is_lvalue_reference<decltype(t1)>::value);
 
 int i1 = 4;
 auto i2 = i1;
 decltype(auto) i3 = i1;
 decltype(auto) i4{i1};
 decltype(auto) i5 = (i1);
 static_assert(!std::is_reference<decltype(i2)>::value);
 static_assert(!std::is_reference<decltype(i3)>::value);
 static_assert(!std::is_reference<decltype(i4)>::value);
 static_assert(std::is_lvalue_reference<decltype(i5)>::value);
 
 auto n1 = non_reference();
 decltype(auto) n2 = non_reference();
 auto&& n3 = non_reference();
 static_assert(!std::is_reference<decltype(n1)>::value, "");
 static_assert(!std::is_reference<decltype(n2)>::value, "");
 static_assert(std::is_rvalue_reference<decltype(n3)>::value, "");
 
 auto l1 = lvalue_reference();
 decltype(auto) l2 = lvalue_reference();
 auto&& l3 = lvalue_reference();
 static_assert(!std::is_reference<decltype(l1)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(l2)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(l3)>::value, "");
 
 auto c1 = const_lvalue_reference();
 decltype(auto) c2 = const_lvalue_reference();
 auto&& c3 = const_lvalue_reference();
 static_assert(!std::is_reference<decltype(c1)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(c2)>::value, "");
 static_assert(std::is_lvalue_reference<decltype(c3)>::value, "");
 
 auto r1 = rvalue_reference();
 decltype(auto) r2 = rvalue_reference();
 auto&& r3 = rvalue_reference();
 static_assert(!std::is_reference<decltype(r1)>::value, "");
 static_assert(std::is_rvalue_reference<decltype(r2)>::value, "");
 static_assert(std::is_rvalue_reference<decltype(r3)>::value, "");
}

用auto定义的变量都是int类型,无论函数的返回类型的引用和const修饰;用decltype(auto)定义的变量的类型与函数返回类型相同;auto&&是转发引用,n3类型为int&&,其余与decltype(auto)相同。

C++17表达式类别

众所周知,编译器常会执行NRVO(named return value optimization),减少一次对函数返回值的移动或拷贝。不过,这属于C++标准说编译器可以做的行为,却没有保证编译器会这么做,因此客户不能对此作出假设,从而需要提供一个拷贝或移动构造函数,尽管它们可能不会被调用。然而,并不是所有情况下都能提供移动构造函数,即使能移动构造函数也未必只是一个指针的交换。总之,我们明知移动构造函数不会被调用却还要硬着头皮提供一个,这样做非常形式主义。

所以,C++17规定了拷贝省略,确保在以下情况下,即使拷贝或移动构造函数有可观察的效果,它们也不会被调用,原本要拷贝或移动的对象直接在目标位置构造:

  • 在return表达式中,运算数是忽略cv修饰符以后的返回类型的prvalue;
  • 在初始化中,初始化器是与变量相同类型的prvalue。

值得一提的是,这类行为在C++17中不能算是一种优化,因为不存在用来拷贝或移动的临时对象。事实上,C++17重新定义了表达式类别:

  • glvalue的求值能确定对象、位域、函数的身份;
  • prvalue的求值初始化对象或位域,或计算运算数的值,由上下文决定;
  • xvalue是表示一个对象或位域的资源能被重用的glvalue;
  • lvalue是不是xvalue的glvalue;
  • rvalue是prvalue或xvalue。

这个定义在功能上与C++11中的相同,但是更清晰地指出了glvalue和prvalue的区别——glvalue产生地址,prvalue执行初始化。

prvalue初始化的对象由上下文决定:在拷贝省略的情形下,prvalue不曾有关联的对象;其他情形下,prvalue将产生一个临时对象,这个过程称为临时实体化(temporary materialization)。

临时实体化把一个完全类型的prvalue转换成xvalue,在以下情形中发生:

  • 把引用绑定到prvalue上;
  • 类prvalue被获取成员;
  • 数组prvalue被转换为指针或下标取元素;
  • prvalue出现在大括号初始化列表中,用于初始化一个std::initializer_list<T>;
  • 被使用typeid或sizeof运算符;
  • 在语句expr;中或被转换成void,即该表达式的值被丢弃。

或者可以理解为,所有非拷贝省略的场合中的prvalue都会被临时实体化。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class NonMoveable
{
public:
 int i = 1;
 NonMoveable(int i) : i(i) { }
 NonMoveable(NonMoveable&&) = delete;
};
 
NonMoveable make(int i)
{
 return NonMoveable{i};
}
 
void take(NonMoveable nm)
{
 return static_cast<void>(nm);
}
 
int main()
{
 auto nm = make(2);
 auto nm2 = NonMoveable{make(3)};
 // take(nm);
 take(make(4));
 take(NonMoveable{make(5)});
}

NonMoveable的移动构造函数被声明为delete,于是拷贝构造函数也被隐式delete。在auto nm = make(2);中,NonMoveable{i}为prvalue,根据拷贝省略的第一条规则,它直接构造为返回值;返回值是NonMoveable的prvalue,与nm类型相同,根据第二条规则,这个prvalue直接在nm的位置上构造;两部分结合,该声明式相当于NonMoveable nm{2};。

在MSVC中,这段代码不能通过编译,这是编译器未能严格遵守C++标准的缘故。然而,如果在NonMoveable的移动构造函数中添加输出语句,程序运行起来也没有任何输出,即使在Debug模式下、即使用C++11标准编译都如此。这也侧面反映出拷贝省略的意义。

总结

C++11规定每个表达式都属于lvalue、xvalue和prvalue三个类别之一,表达式另可分为lvalue和rvalue,或glvalue和prvalue。返回右值引用的函数调用是xvalue,右值引用类型的变量是lvalue。

const左值引用和右值引用可以绑定临时对象,但是临时对象的声明周期只能延长一次,返回一个指向局部变量的右值引用也会导致空悬引用。

标识符加上一对圆括号成为表达式,decltype用于表达式可以根据其类别产生相应的类型,用decltype(auto)声明变量可以保留表达式类别。

C++17中prvalue是否有关联对象由上下文决定,拷贝省略规定了特定情况下对象不经拷贝或移动直接构造,NRVO成为强制性标准,使不能被移动的对象在语义上可以值传递。

参考

Value categories - cppreference.com

Value categories - [l, gl, x, r, pr]values

Value Categories in C++17

到此这篇关于C++98/11/17表达式类别的文章就介绍到这了,更多相关C++98/11/17表达式类别内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/jerry-fuyi/p/12927532.html

延伸 · 阅读

精彩推荐