最近在看C++20相关的内容,本篇记录下遇到的比较好用的特性
C++20新增的4个大特性之一,Module解决的是以前C编译include预处理效率低下痛点。提案地址,具体内容单独介绍,需要了解的事 module,import也成为关键字了,C++20之前的代码最好不要使用,防止冲突。
推荐的格式化字符串方法
C++ allows you to include an initializer inside an if statement using the following syntax:
if (<initializer>; <conditional_expression>) { <if_body>
} else if (<else_if_expression>) { <else_if_body>
} else { <else_body>
}
新增的运算符 <=>,结果如下:
strong_ordering::less: First operand less than second
strong_ordering::greater: First operand greater than second
strong_ordering::equal: Equal operands
对于符点:
partial_ordering::less: First operand less than second
partial_ordering::greater: First operand greater than second
partial_ordering::equivalent: Equal operands
partial_ordering::unordered: If one or both of the operands is not-a-number
对于自定义类型,也可以返回以下类型:
weak_ordering::less: First operand less than second
weak_ordering::greater: First operand greater than second
weak_ordering::equivalent: Equal operands
<compare>也提供了其他的方法可以使用: is_eq(), is_neq(), is_lt(), is_lteq(), is_gt(), and is_gteq()
类比于constexpr 可能在编译期间执行,consteval可以保证会在编译期间执行
const char *的替代品,只读字符串,也同时支持std::string的功能
输出会是如下的样子:
image.png