Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using C++11 attributes

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?

like image 227
innochenti Avatar asked Mar 25 '12 21:03

innochenti


People also ask

What is the use of attribute in C?

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.

What is attribute in C and example?

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.


2 Answers

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... ;)

like image 54
R. Martinho Fernandes Avatar answered Oct 07 '22 23:10

R. Martinho Fernandes


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.

like image 45
Tom Avatar answered Oct 08 '22 00:10

Tom