-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
55 lines (45 loc) · 1.29 KB
/
test.cpp
File metadata and controls
55 lines (45 loc) · 1.29 KB
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
/**
* @file test.cpp
* @author your name (you@domain.com)
* @brief const volatile mutable
* @version 0.1
* @date 2024-10-23
*
* @copyright Copyright (c) 2024
*
*/
#include <iostream>
using namespace std;
/// @brief 实参不为const时, 形参为const, 此时实参可直接通过const_cast修改
/// @param val
void test1(const int& val) {
const_cast<int&>(val)++;
//val++;
}
/// @brief 实参为const时, 形参也必须为const, 此时实参要带volatile才可通过const_cast修改
/// @param val
void test2(volatile const int& val) {
const_cast<int&>(val)++;
}
int main() {
int a = 0;
const int b = 0;
auto f1 = [=]() {
//a++; // 错误,不提供说明符时复制捕获的对象在 lambda 体内是 const 的。
//b++; // 错误,同上,且按值传递const也会传递进来
return a;
};
auto f2 = [=]() mutable { // 提供mutable说明符
a++; // 正确,mutable解除const限制。
/*
b++; // 错误,mutable无法突破b本身的const限制
*/
return a;
};
cout << a << ", " << b << endl; // 输出0, 0
cout << f1() << ", " << f2() << endl; // 输出0, 1
int c = 1;
test1(c);
std::cout << c << std::endl;
return 0;
}