一元作用域解析运算符
局部变量名与全局变量名相同时,可使用 ::
访问全局变量,::
这个运算符被称为一元作用域解析运算符。
int v1 = 10;
int main() {
int v1 = 5;
std::cout << "local variable v1 is " << v1 << std::endl;
std::cout << "global variable v1 is " << ::v1 << std::endl;
return 0;
}
趋向运算符
#include <iostream>
int main(int argc, char *argv[]) {
auto i = int{3};
while (i --> 0) {
std::cout << i << std::endl; // 输出: 2 1 0
}
return EXIT_SUCCESS;
}
等价于:
auto i = int{3};
while (i >= 0) {
std::cout << --i << std::endl; // 输出: 2 1 0
}
还有另外一种写法:
auto i = int{3};
while (0 <-- i) {
std::cout << i << std::endl; // 输出: 2 1
}
等价于:
auto i = int{3};
while (i > 0) {
std::cout << --i << std::endl; // 输出: 2 1
}
switch范围判断
#include <iostream>
int main(int argc, char *argv[]) {
auto i = uint32_t{2};
switch (i) {
case 0 ... 3: // 之前写法:case 0: case 1: case 2: case 3:
std::cout << "0-3" << std::endl;
break;
default:
break;
}
return EXIT_SUCCESS;
}
网友评论