C++ source_annotation_attribute

C++ source_annotation_attribute

本文关键字:attribute annotation C++ source      更新时间:2023-10-16

在浏览开源.NET Framework 4.7的源代码时,我偶然发现了C++标头sal.h,并发现了一行代码,上面写着[source_annotation_attribute( SA( Method ) )],这似乎类似于C#中的属性和AttributesUsage类。

现在我知道,一般来说,C++中没有像 C# 那样的用户定义属性,我的第一个猜测是[source_annotation_attribute( SA( Method ) )]只是一个宏,但它既没有在sal.h中定义,也没有在任何其他标头中定义,因为sal.h没有#include任何。

我的下一个猜测是[source_annotation_attribute]实际上是在 MSVC 中构建的,就像例如[[noreturn]]属性。

如果有人能阐明它实际上是什么,并且如果我可以声明我自己的类似属性,如果它没有内置到编译器中,我会很高兴。 如果您想自己查看,则Sourceexternalapislegacyvctoolsvc12incvcsal.h特定文件,并且该属性(除其他外(出现在第1934行中。

以下是sal.h中的用法示例:

[source_annotation_attribute( SA( Method ) )]
struct __M_
{
#ifdef __cplusplus // [
__M_();
#endif // ]
int __d_;
};
typedef struct __M_ __M_;

提前非常感谢。

总结@VTT已经说过的内容,看起来source_annotation_attribute是一个编译器内置结构,它是作为Microsoft扩展的一部分提供的C++ (即使那里没有提到它,因为它是一个实现细节,仅供内部使用(仅在使用编译器开关/Ze编译时才有效

除此之外,微软的SAL是在Visual Studio中深深内置的。 即

生成 -> 对解决方案运行代码分析 由于Visual Studio(显然(使用他们的MSVC编译器,因此Microsoft不会在编译器中构建任何这样的内部结构并不是太不可信。