源码

首页 » 归档 » 源码 » C++标准委员会7月科隆会议中投票通过的特性

C++标准委员会7月科隆会议中投票通过的特性

引言

上周 C++ 标准委员会在科隆举行了7月会议(WG21)。 会议报告请戳:《Trip Report: C++ Standards Meeting in Cologne, July 2019》

会议中重点针对 C++20 标准已经投票通过的特性进行了若干修正,并对一些草案进行了讨论和投票。相比上次会议,通过了一些新的草案。

移除的语言特性

合约(Contracts)

新增的主要语言特性

类模板参数推导优化

constexpr 容器的语言支持

constinit 关键字

using enum

在大多数场景下废弃 volatile 关键字

新增的主要标准库特性

文本格式化支持

C++20 Synchronization Library

Input Range 适配器

constexpr std::vector

constexpr std::string

线程可中断 join 支持(Stop Token)

std::source_location

概念(Concepts)采用 C++ 标准库命名规范

constexpr std::invoke

新增特性介绍

上述特性是本人认为的本次会议投票通过的主要特性。若要浏览全部特性列表,请戳引言中的连接。

下面我将其中一些关键特性作简要介绍。

constinit 关键字

这是新引入的一个关键字,用于强制标记一个全局变量的初始化在编译期完成。若初始化表达式无法在编译期求值,则会引发编译错误。这样便可以避免全局变量的隐式运行时初始化带来的各种各样难以调试的 BUG。

示例:

const char* g() { return "运行时初始化"; }constexpr const char* f(bool p) { return p ? "常量初始化" : g(); }
constinit auto c = f(true); // OKconstinit auto d = f(false); // 编译错误

using enum

这个特性算是对现有 using 语句用法的一个功能补全。它允许 using 语句运用在 enum 和 enum class 之中。

给出枚举:

// china_railway.hppenum class china_railway{
cr400af,
cr400bf,
cr200j
};

用法1:

using enum china_railway;
constexpr auto train_type = cr400af;

用法2:

struct foo{
// 引入枚举成员。
using enum china_railway;
};void geek(){
foo f;auto a = f.cr400af; // 使用类实例,合法
auto b = foo::cr200j; // 使用类名,合法}

用法3:

using china_fuxing_trains = china_railway;using rubbish = china_railway::cr200j; // 枚举值别名constexpr auto type = china_fuxing_trains::cr400bf;constexpr auto fake_emu_train = rubbish; // 和 cr200j 等价

文本格式化支持

这是继模块、协程和概念后又一个重磅特性。它弥补了 C++ 标准库缺乏文本格式化支持的一个遗憾。这次通过的提案基于开源库 fmt,语法十分优雅。文本格式化主要通过两个新的标准库函数 std::format 和 std::format_to 来实现。

示例1:std::format 基本用法

// 自动编号auto text1 = std::format("{} 是动车组,但 {} 却不是。", "CR400AF", "CR200J");// 手动编号auto text2 = std::format("我国领土面积 {0} 万平方公里,人口有 {1} 亿。", 960, 14);// 取消“{}”的转义。// text3 = "我们的分组是:{100, 200, 300}。"auto text3 = std::format("我们的分组是:{{{0}, {1}, {2}}}。", 100, 200, 300);

示例2:std::format 格式化控制

std::format 函数支持高级格式化控制。它使用格式控制表达式进行格式化输出。标准格式控制表达式的形式如下所示。

"{[arg_index]:[[fill]align][sign]['#']['0'][width]['.' precision][type]}"

看似很复杂的亚子,其实不难,让我们通过如下的表格来解析。

说明:上式中的方括号[]代表为可选参数。

1.PNG

怎么样,是不是通过上表的说明,大家对这个格式控制有了一个初步的理解呢?下面我们继续展开,深入讲讲 align、sign 和 type参数取值的具体意义。

表格1: align 参数

2.PNG

表格3: type 参数说明

3.PNG

写到这,不得不感叹,这次添加进标准的格式化函数还是非常完备的,考虑到了各种类型的格式化。理论用于实际,让我们看看一些实例吧。

// 高级用法:格式化控制符char c = 120;auto s0 = std::format("{:6}", 42);      // s0 == "    42"auto s1 = std::format("{:6}", 'x');     // s1 == "x     "auto s2 = std::format("{:*<6}", 'x');   // s2 == "x*****"auto s3 = std::format("{:*>6}", 'x');   // s3 == "*****x"auto s4 = std::format("{:*^6}", 'x');   // s4 == "**x***"auto s6 = std::format("{:6d}", c);      // s6 == "   120"auto s7 = std::format("{:=+06d}", c);   // s7 == "+00120"auto s8 = std::format("{:0=#6x}", 0xa); // s8 == "0x000a"auto s9 = std::format("{:6}", true);    // s9 == "true  "

示例3:std::format_to 的用法

std::format_to 函数主要用于容器类型,如 std::vector 的格式化添加值。

#include #include #include int main(){std::vector data;for (size_t i = 0; i < 100; i++)
{std::format_to(std::back_inserter(data), "我的工号是:{} ", i);
}
}

其中格式字符串的用法等同于 std::format 函数,可参考上述有关内容。

std::source_location

这是一个比较实用的特性。它为标准库新增了一个元数据类 std::source_location,可为用户提供当前代码的上下文信息。该类的定义如下所示。

namespace std{struct source_location
{
constexpr source_location() noexcept;constexpr uint_least32_t line() const noexcept;constexpr uint_least32_t column() const noexcept;constexpr const char* file_name() const noexcept;constexpr const char* function_name() const noexcept;static consteval source_location current() noexcept;
};
}

示例:

#include #include #include void foo(){// Get the location of this line.
auto loc = std::source_location::current();
std::cout << std::format("Line: {}, Column: {}, Function: {}, File: {}", loc.line(), loc.column(), loc.function_name(), loc.file_name()) << std::endl;
}

概念(Concepts)采用标准库命名规范

这个特性是指原本采用 Pascal 命名法的概念,在这次会议中投票通过改为与标准库一致的小写下划线命名法。如:

std::Invocable 改为 std::invocable

std::ConvertibleTo 改为 std::convertible_to

std::EqualityComparable 改为 std::equality_comparable

等等

总结

由于这次投票通过的特性还是比较多的,我只挑选了几个有代表性的展开,其他的就不再一一赘述。(

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/cbiaozhunweiyuanhui7yuekelonghuiyizhongtoupiaotongguodetexing.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:8 月 7, 2019 at 03:04 上午

热评文章