使用 C++11 属性

2022-01-13 00:00:00 attributes c++ c++11

您能否解释一下如何从 C++ 中的属性中获取信息?例如,我想编写 C++ 到 python 绑定.为此,我需要用一些特定的绑定信息来注释类中的所有方法.现在我需要按属性生成一些代码.或者另一个例子,将类映射到数据库实体.还是 C++11 的属性与 Java 或 C# 中的注解不一样?

Could you please explain how to get information from attributes in C++? For example, I want to write C++ to python binding. For this I need to annotate all methods in class with some specific binding info. Now I need to generate some code by attributes. Or another example, map class to db entity. Or C++11 attributes is not the same as in Java or C# annotations?

推荐答案

属性(新的 C++11 功能) 只是编译器扩展的标准化语法.要执行您想要的操作,您需要一个具有适当扩展名的编译器.到目前为止,我认为任何编译器都没有实现属性语法,更不用说 Python 绑定的任何特定属性了.

Attributes (a new C++11 feature) are just a standardized syntax for compiler extensions. To do what you want you would need a compiler with the proper extensions. So far, I don't think any compiler even implements attribute syntax, much less any specific attributes for Python bindings.

因为它们是为编译器扩展而设计的,所以没有创建自己的属性的标准方法,就像使用 Java 注释或 C# 属性一样.当然,编译器可以提供这种能力作为扩展... ;)

Because they're intended for compiler extensions, there's no standard way of creating your own attributes, like you can with Java annotations or C# attributes. Of course, a compiler could provide this ability as an extension... ;)

相关文章