[cfe-dev] Attributes on statements and expressions

Trevor Harmon Trevor.W.Harmon at nasa.gov
Tue Apr 27 14:35:59 PDT 2010


On Apr 27, 2010, at 1:37 AM, Olaf Krzikalla wrote:

>> There's one thing I'm confused about, though. I assume it's possible
>> to define my own attributes, e.g. [[mycoolattr(42)]]. But how exactly
>> do I do that? Is there some sample code somewhere?
> I don't know of examples. The FCD states that the behavior of  
> attributes
> other than the predefined ones are implementation defined. That is you
> can define your own attributes at will in the compiler implementation.

I'm only familiar with the Java mechanism, which works like this:

// Definition of custom attribute
public @interface RequestForEnhancement {
     String synopsis();
     String engineer() default "[unassigned]";
     String date() default "[unimplemented]";
}

// Use of custom attribute
@RequestForEnhancement(
     synopsis = "Enable time-travel",
     engineer = "Mr. Peabody",
     date = "4/1/3007" )
public static void travelThroughTime(Date destination) { ... }

The nice thing about this is that the annotation data is (optionally)  
embedded into the Java class file's metadata, and it can then be  
extracted by tools by means of a well-defined spec.

Is there a way to do something similar to the above in C++0x?

Trevor




More information about the cfe-dev mailing list