C++17的新特征

结构化绑定

结构化绑定就是将指定的名称和初始化器的子对象或元素绑定。
与引用一样,结构化绑定是现有对象的别名。与引用不同的是,结构化绑定不必是引用类型。

下面展示一些 内联代码片

attr(optional) cv-auto ref-qualifier(optional) [ identifier-list ] = expression ;	   (1)	
attr(optional) cv-auto ref-qualifier(optional) [ identifier-list ] {
    expression } ;	(2)	
attr(optional) cv-auto ref-qualifier(optional) [ identifier-list ] ( expression ) ;	(3)	

attr 任意数量属性的序列
cv-auto 可能是 cv 限定的类型说明符是,auto,也可能包括存储类说明符,static 或 thread_local
ref-qualifier & 或者 &&
identifier-list 此说明引入的,逗号分隔的标识符列表
expression 是一个表达式,该表达式在顶层,没有逗号运算符(语法上,是赋值表达式),并且具有数组或非联合类类型。如果表达式引用标识符列表中的任何名称,则说明格式是错误的。

绑定数组

标识符列表中的每个标识符都成为一个左值的名称,该左值引用数组的相应元素。标识符的数量必须等于数组元素的数量。
每个标识符的引用类型是数组元素类型。如果数组类型 E 是 cv 限定的,那么它的元素类型也是如此。

例子

int a[2] = {
   1,2};
 
auto [x,y] = a; // creates e[2], copies a into e, then x refers to e[0], y refers to e[1]
auto& [xr, yr] = a; // xr refers to a[0], yr refers to a[1]

绑定一个tuple类型

表达式 std::tuple_size::value 必须是良定义的整数常量表达式,并且,标识符的数量必须等于 std::tuple_size::value。
对于每个标识符,引用一个变量,该变量的类型是,引入一个类型为,std::tuple_element<i, E>::type 的引用。如果其对应的初始值设定项是左值,则为左值引用,否则为右值引用。第 i 个变量的初始化器是

  • e.get(),如果通过类成员访问查找在 E 范围内查找标识符,get 发现至少一个说明是函数模板,其第一个模板参数是非类型参数
  • 否则,get(e),其中 get 仅通过依赖于参数的查找来查找,忽略非 ADL 查找。

在这些初始化器表达式中,如果实体 e 的类型是左值引用,则 e 是左值(仅当 ref 限定符是 & 或 &&, 并且初始化器表达式是左值时,才会发生这种情况),否则是 xvalue(这有效地执行一种完美转发),i 是 std::size_t prvalue,并且 <i> 总是被解释为模板参数列表。
该变量具有与 e 相同的存储持续时间。
该标识符然后成为一个左值的名称,该左值引用绑定到所述变量的对象。
第 i 个标识符的引用类型是 std::tuple_element<i, E>::type。

例子

float x{
   1.0};
char  y{
   ‘c'};
int   z{
   5};
 
std::tuple<float&,char&&,int> tpl(x,std:<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 并发 C++11 C++17 I encountered the concept of multithreaded code while working at my first job after I left college. We were writing a data processing application that had to populate a database with incoming data records. There was a lot of data, but each record was independent and required a reasonable amount of processing before it could be inserted into the database. To take full advantage of the power of our 10-CPU UltraSPARC, we ran the code in multiple threads, each thread processing its own set of incoming records. We wrote the code in C++, using POSIX threads, and made a fair number of mistakes—multithreading was new to all of us—but we got there in the end. It was also while working on this project that I first became aware of the C++ Standards Committee and the freshly published C++ Standard. I have had a keen interest in multithreading and concurrency ever since. Where others saw it as difficult, complex, and a source of problems, I saw it as a powerful tool that could enable your code to take advantage of the available hardware to run faster. Later on I would learn how it could be used to improve the responsiveness and performance of applications even on single-core hardware, by using multiple threads to hide the latency of time-consuming operations such as I/O. I also learned how it worked at the OS level and how Intel CPUs handled task switching. Meanwhile, my interest in C++ brought me in contact with the ACCU and then the C++ Standards panel at BSI, as well as Boost. I followed the initial development of the Boost Thread Library with interest, and when it was abandoned by the original developer, I jumped at the chance to get involved. I have been the primary developer and maintainer of the Boost Thread Library ever since.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值