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?
The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.
Attributes are a mechanism by which the developer can attach extra information to language entities with a generalized syntax, instead of introducing new syntactic constructs or keywords for each feature.
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.
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... ;)
An update with some more recent information:
GCC now (as of 4.8) implements C++11 attributes as an alternative syntax for __attribute__((XXX))
.
You can also use the GCC plugin mechanism to define new attributes - see https://gcc.gnu.org/onlinedocs/gccint/Plugins.html.
You can also do this in python using the gcc-python-plugin - see https://gcc-python-plugin.readthedocs.org/en/latest/attributes.html.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With