网站首页 > 博客文章 正文
指定初始化
现在可以初始化特定(指定的)聚合成员并跳过其他成员。与C语言不同,初始化顺序必须与聚合声明相同。
#include <iostream>
#include <string>
struct S{
int x;
int y{2};
std::string s;
};
int main()
{
S s1{.y = 3}; // {0, 3, {}}
S s2 = {.x = 1, .s = "abc"}; // {1, 2, {"abc"}}
//S s3{.y = 1, .x = 2}; // Error, x should be initialized before y
std::cout << s1.y << std::endl;
return 0;
}
https://wandbox.org/nojs/gcc-head
https://wandbox.org/nojs/clang-head
位域的默认成员初始化器
在C++ 20之前,要为位域提供默认值,必须创建默认构造函数,现在可以使用方便的默认成员初始化语法来实现。
#include <iostream>
#include <string>
#include <stdio.h>
// until C++20:
struct S1{
int a : 1;
int b : 1;
S1() : a{0}, b{1}{}
};
// since C++20:
struct S2{
short int a : 1 {0};
short int b : 1 {1};
};
int main()
{
S1 s1;
S2 s2;
std::cout << sizeof(s1) << std::endl;
printf( "S2.b : %hhu\n", s1.b );
std::cout << sizeof(s2) << std::endl;
printf( "S2.b : %hhu\n", s2.b );
return 0;
}
typename更多的选项
typename可以在只出现类型名的上下文中省略(类型转换、返回类型、类型别名、成员类型、成员函数的参数类型等)。
嵌套的内联名称空间
inline关键字允许出现在嵌套的命名空间定义中:
#include <iostream>
#include <string>
#include <stdio.h>
// C++20
namespace A::B::inline C1{
void f(){std::cout << "C1" << std::endl;}
}
// C++17
namespace A::B{
inline namespace C2{
void f(){std::cout << "C2" << std::endl;}
}
}
int main()
{
A::B::C1::f();
A::B::C2::f();
return 0;
}
using enum
作用域枚举很棒,唯一的问题是它们的冗长使用(例如my_enum::enum_value)。例如,在检查每个可能的enum值的switch语句中,应该为每个case-label重复my_enum:: part。使用enum声明将所有枚举名称引入当前作用域,因此它们作为非限定名称可见,my_enum::部分可以省略。它可以应用于无作用域枚举,甚至单个枚举器。
#include <iostream>
#include <string>
#include <stdio.h>
namespace my_lib {
enum class color { red, green, blue };
enum COLOR {RED, GREEN, BLUE};
enum class side {left, right};
}
void f(my_lib::color c1, my_lib::COLOR c2){
using enum my_lib::color; // introduce scoped enum
using enum my_lib::COLOR; // introduce unscoped enum
using my_lib::side::left; // introduce single enumerator id
// C++17
if(c1 == my_lib::color::red){std::cout << "red" << std::endl;}
// C++20
if(c1 == green){/*...*/}
if(c2 == RED){std::cout << "RED" << std::endl;}
auto r = my_lib::side::right; // qualified id is required for `right`
auto l = left; // but not for `left`
}
int main()
{
f(my_lib::color::red, my_lib::COLOR::RED);
return 0;
}
new 表达式中的数组大小推到
这个修复允许编译器推导new表达式中的数组大小,就像它对局部变量所做的那样。
#include <iostream>
#include <string>
#include <stdio.h>
int main()
{
// before C++20
int p0[]{1, 2, 3};
int* p1 = new int[3]{1, 2, 3}; // explicit size is required
// since C++20
int* p2 = new int[]{1, 2, 3};
int* p3 = new int[]{}; // empty
char* p4 = new char[]{"hi"};
// works with parenthesized initialization of aggregates
int p5[](1, 2, 3);
int* p6 = new int[](1, 2, 3);
return 0;
}
为别名模板推导类模板参数
#include <iostream>
#include <string>
#include <stdio.h>
template<typename T>
using IntPair = std::pair<int, T>;
int main()
{
double d{};
IntPair<double> p0{1, d}; // C++17
IntPair p1{1, d}; // std::pair<int, double> // C++20
IntPair p2{1, p1}; // std::pair<int, std::pair<int, double>>
return 0;
}
猜你喜欢
- 2025-08-03 C++语法进阶-字符:字符变量(char)
- 2025-08-03 c++26新功能—Read-Copy-Update
- 2025-08-03 为什么Linux之父那么讨厌C++ 他骂的这几点。句句扎心
- 2025-08-03 为什么Linux之父那么讨厌C++ 他骂的这几点!句句扎心
- 2025-08-03 20道qiao牛逼的c++/c面试题
- 2025-08-03 C++学习教程_C++语言随到随学_不耽误上班_0基础
- 2025-08-03 20天轻松入门《C++第四章——函数》——4经坛教育
- 2025-08-03 看完侯捷老师所有C++视频之后的总结
- 2025-08-03 C++11+ 泛型编程(模板)
- 2025-08-03 C++20并发库新成员jthread(续)
你 发表评论:
欢迎- 08-06nginx 反向代理
- 08-06跨表插入连续的日期,sheetsname函数#excel技巧
- 08-06初中生也能学的编程,不走弯路,先用后学
- 08-06find命令的“七种武器”:远不止-name和-type
- 08-06恶意代码常见的编程方式
- 08-06kali2021ping 外网不通
- 08-06因为一个函数strtok踩坑,我被老工程师无情嘲笑了
- 08-06hadoop集群搭建详细方法
- 22℃nginx 反向代理
- 最近发表
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- powershellfor (73)
- messagesource (71)
- plsql64位 (73)
- vueproxytable (64)
- npminstallsave (63)
- #NAME? (61)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- nacos启动失败 (64)
- ssh-add (70)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- qcombobox样式表 (68)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)