Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java-like annotations in C++

Is there something like Java's annotations in C++ ?

For example, the @Override annotation marks a function that it overrides another function, and if it wouldn't, it would give an error at compile time.

I am looking for something like this in C++.

like image 789
clamp Avatar asked Jan 14 '11 09:01

clamp


People also ask

Are there annotations in C++?

The C++ Annotations are also available as a Kindle book. Don't hesitate to send in feedback: send an e-mail if you like the C++ Annotations; if you think that important material was omitted; if you find errors or typos in the text or the code examples; or if you just feel like e-mailing. Send your e-mail to Frank B.

What is the use of @interface annotation?

Notice the @interface keyword. This signals to the Java compiler that this is a Java annotation definition. Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name.

What is @interface annotation in Java?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.

Why do we use @override annotation?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.


1 Answers

C++11 provides support for generalized attributes, which can be seen as superset of Java annotations, as they can be applied not just to variables/functions, but also to statements, for example. But C++11 defines only syntax for generalized attributes, not means for user to define them.

This article gives good overview of generalized attributes: http://www.codesynthesis.com/~boris/blog/2012/04/18/cxx11-generalized-attributes/

GCC supports this feature from version 4.8, according to: http://gcc.gnu.org/projects/cxx0x.html

To implement support for user-defined attributes, compiler plugins are promising, especially based on high-level language integration, like https://fedorahosted.org/gcc-python-plugin/

like image 84
pfalcon Avatar answered Oct 04 '22 17:10

pfalcon