Home > Blogs > Attributes Related Issues

Attributes Related Issues

By  Sep 14, 2009

Topics: Programming, C/C++

People ask me for my opinion about attributes. Unlike with concepts (and several other harmful features that I've criticized before), I'm quite neutral about attributes. I don't think they're the best feature of C++0x nor are they the most sought-after feature but they certainly have the potential of solving certain programming issues that have hitherto been too loosely defined in the Standard, e.g., alignment.  

Much of the debate about attributes can be attributed (pun unintended) to their underspecification. Presently, there are still several significant issues that need to be resolved before the typical C++ programmer (and of course the standards committee) can make up their minds about this controversial feature. Here are some of these issues.

What are Attributes?

If you look at the papers that present this feature, none of them tells exactly what attributes should be. The vague characterization (lack of semantic content) is neither accurate nor clear cut. We need a more formal definition that will enable us to test objectively whether a certain attribute is valid or not.

Type Modification

According to the latest draft, attributes may appear in a function's parameter list. Under these circumstances, it's only natural to ask whether attributes can modify types and affect overload resolution. For example,

void func( C [[align(16)]] c); //#1

void func( C c);//#2

C obj;

func (obj); //is the call ambiguous or should #2 be called?

The same question applies to template argument matching -- do attributes affect it?

Attributes Introducer

The current introducer is [[]] but as we all know, this syntax isn't favorable with many C++ users and committee members. Is the [[]] introducer here to stay or is it time to reconsider it, especially when the recent months have taught us that attributes are going to become more widely-used than originally predicted.

Attributes Lists

This is almost preaching to choir, but I still believe it's worth mentioning. The current proposal doesn't let you include multiple attributes per declaration. In the future, it will be allowed, e.g.,

void func( C [[align(16), xyz::noalias, nocaching]] c );  

Empty Attributes

This is no longer an issue. According to current draft, an empty attribute is valid and has no effect. I'm only mentioning it in order to dispel any doubts about cases such as the following:

class D [[]] {}; //same as class D {};

void B::func [[]] (); //same as void B::func();

Become an InformIT Member

Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.