一、属性
其实属性这个词出现很早,属性这个词在不同的环境下有不同的语义。在英文中体现为一个是“Property”一个是“Attribute”,在本文中,是对后者的说明。属性,其实可以认为是一种对象的基础特征。有过Linux下编程的都知道,在GUN C中,有编程语言扩展 attribute((…))关键字,而在Windows平台上有编程语言扩展 __declspec() 等语法。统一的属性关键字在Java、c#等语言中早就普及使用,但在c++标准中,仍然没有支持,一直到c++11才有了改观。
二、c++20中的属性
从c++11始到c++20,规则不断完善,提供了以双中括号显示指定的语法标志,如下:
Introduces implementation-defined attributes for types, objects, code, etc.
[[attr]] [[attr1, attr2, attr3(args)]] [[namespace::attr(args)]] alignas_specifier
Formally, the syntax is
[[ attribute-list ]] (since C++11)
[[ using attribute-namespace : attribute-list ]] (since C++17)
where attribute-list is a comma-separated sequence of zero or more attributes (possibly ending with an ellipsis ... indicating a pack expansion)
identifier (1)
attribute-namespace :: identifier (2)
identifier ( argument-list ) (3)
attribute-namespace :: identifier ( argument-list ) (4)
1) simple attribute, such as [[noreturn]]
2) attribute with a namespace, such as [[gnu::unused]]
3) attribute with arguments, such as [[deprecated("because")]]
4) attribute with both a namespace and an argument list
If using namespace: appears in the beginning of an attribute list, no other attributes in the attribute list can specify a namespace: the namespace specified in a using applies to them all:
[[using CC: opt(1), debug]] // same as [[CC::opt(1), CC::debug]]
[[using CC: CC::opt(1)]] // error: cannot combine using and scoped attribute(since C++17)
在c++的标准中定义了如下的属性:
C++ 标准仅定义下列属性。
[[noreturn]](C++11) 指示函数不返回
[[carries_dependency]](C++11) 指示释放消费 std::memory_order 中的依赖链传入和传出该函数。
[[deprecated]](C++14)
[[deprecated("原因")]](C++14) 指示允许使用声明有此属性的名称或实体,但因 原因 而不鼓励使用。
[[fallthrough]](C++17) 指示从前一 case 标号直落是有意的,而在发生直落时给出警告的编译器不应该为此诊断。
[[nodiscard]](C++17)
[[nodiscard("原因")]](C++20) 鼓励编译器在返回值被舍弃时发布警告。
[[maybe_unused]](C++17) 压制编译器在未使用实体上的警告,若存在。
[[likely]](C++20)
[[unlikely]](C++20) 指示编译器应该针对通过某语句的执行路径比任何其他执行路径更可能或更不可能的情况进行优化。
[[no_unique_address]](C++20) 指示非静态数据成员不需要拥有不同于其类的所有其他非静态数据成员的地址。
[[optimize_for_synchronized]](TM TS) 指示应该针对来自 synchronized 语句的调用来优化该函数定义
在GCC和Clang中定义的相关属性可参看如下资料:
https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html#Attribute-Syntax
https://clang.llvm.org/docs/AttributeReference.html
其实使用属性的优点还是非常明显的,一些常用的变量或者语法,可以直接指定而不需要在程序内不断的重复相关的动作和代码,显得代码更清晰整洁,易于理解。
三、例程
在前面分支预测其实就是一个属性的例子,这里再看几个:
//nodiscard
struct [[nodiscard]] error_info { };
error_info enable_missile_safety_mode();
void launch_missiles();
void test_missiles() {
enable_missile_safety_mode(); // 编译器可在舍弃 nodiscard 值时发布警告
launch_missiles();
}
error_info& foo();
void f1() {
foo(); // 并非按值返回 nodiscard 类型,无警告
}
//fallthrough
void f(int n) {
void g(), h(), i();
switch (n) {
case 1:
case 2:
g();
[[fallthrough]];
case 3: // 直落时不警告
h();
case 4: // 编译器可在发生直落时警告
if(n < 3) {
i();
[[fallthrough]]; // OK
}
else {
return;
}
case 5:
while (false) {
[[fallthrough]]; // 非良构:下一语句不是同一迭代的一部分
}
case 6:
[[fallthrough]]; // 非良构:无后继的 case 或 default 标号
}
}
用上属性,是不是更清晰,不用再写注释让人再去分辨对错了。简单就是美,简单就是王道。
更多细节,请参阅:
https://en.cppreference.com/w/cpp/language/attributes
四、总结
其实越看c++的标准,越看C++新的标准,其实和其它语言当初那锋利的界限越来越模糊,这也是一个不争的事实,也是一种天然的进步。每个语言在迭代过程中,所经历的问题的大抵是相似的,而解决问题的方法和手段在计算机领域内也就那几种,所以殊途同归,最终的表现形式,一定是你中有我,我中有你,不可能再象以前,边边角角的都是那样的棱角分明。
有借鉴,才有创新;有吸收,才会有创新;更合适,就是创新。