Home > Guides > Programming > C/C++

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

The Debate About Attributes, Part I

Last updated Aug 28, 2009.

Attributes were voted into the Working Draft (WD) at the San Francisco meeting in October 2008. That voting wasn't unanimous (29 votes in favor of attributes, 7 against). Certain committee members expressed their concern about various aspects of the attributes proposal and some even said that their implementation wouldn't support the standard attributes at all (for further details, read the minutes from that meeting). The controversy about attributes has intensified recently because the original attributes proposal was extended last month at the Berlin meeting. It appears that my predictions about the aftershocks caused by the removal of concepts are materializing: although attributes are in the Working Draft, committee members who object to them want to see attributes removed from the Working Draft, while at the same time, other members strongly support attributes. In the first part of this series I will present the claims of those who object to attributes. The claims in favor of attributes will be presented in the second part.

Are Attributes Ignorable?

I introduced attributed recently. In the meantime, the committee has added three more attribute tokens to the standard: [[override]], [[base_check]] and [[hiding]]. I will not explain the meaning of the new attribute tokens here (they deserve a column of their own) but I believe that their semantic content is self-explanatory. They're all used in the context of a class hierarchy.

The main argument against the new attribute tokens is that compilers cannot ignore them. In other words, the new tokens (and some of the original attribute tokens) are in fact keywords in disguise. That's a significant change from the original proposal which was voted into the Working Draft. Initially, attributed were described as "just simple annotations that can always be ignored, with no semantic effect". Allow me to elaborate on this claim. Suppose you have a function such as std::exit() that never returns to its caller. According to the original attributes proposal, implementers can annotate such a function with the [[noreturn]] attribute in order to let the compiler optimize the code or suppress spurious warnings. The crucial point is that omitting the [[noreturn]] attribute shouldn't change the semantics of the program -- it will behave in exactly the same way whether exit() is annotated with [[noreturn]] or not. In fact, this criterion was the litmus test for determining whether a certain property should be expressed as an attribute or not. Now examine the [[final]] attribute. It violates the "ignorability" criterion. The compiler can't ignore [[final]]. If the program tries to override a member function that was annotated as such, that program is ill-formed. Put differently, [[fina]] (as well as [[align]], [[override]], [[base_check]] and [[hiding]]) carry semantic content that affects the behavior of the program; they cannot be ignored and they are not optional. Thus, although the original proposal explicitly said: "we were asked to offer guidance in order to prevent wholesale dumping of extension keywords into[...] attribute[s]...", we do effectively have attributes that are equivalent to context-sensitive keywords. You can still use final as an identifier in your code, but the [[final]] attribute works very much like a keyword. In this respect, committee members who have objected to attributes right from the start claim that this is a case of "creeping featurism", as semantics change was explicitly not part of the original intent for attributes. There are of course additional reasons for the objecting to standardized attributes:

The Case Against Attributes

Some of the claims against attributes come from mainstream programmers who were first exposed to the notion of attributes recently. Other claims come from committee members.

Ugly syntax. When I presented the notion of attributes in an article I noticed that the comments left by readers repeatedly expressed concerns about the funky [[attrib]] syntax. Of course, we have other syntactic monstrosities in C++ but there's no reason to add a new monstrosity unless it's completely unavoidable. The question is therefore: are attributes really indispensable?

Standardizing the nonstandard. The attempt to standardize attributes is problematic because vendors have been using attributes (in completely different ways) as a mechanism for specifying nonstandard properties of a certain construct. Each vendor uses a different syntactic form. Seemingly, such a chaotic state of affairs cries for standardization but there's a difference between standardizing only the syntactic form of attributes and standardizing the semantic content (which is usually implementation-dependent). In other words, the best thing the standards committee should do is leave the current state of affairs as-is instead of forcing yet another design-by-committee.

Syntactic confusion. Many vendors already support two(!) separate syntactic forms of attributes. Microsoft's Visual Studio for instance supports both [attribute] and __declspec(attribute) (their attributes are completely different from the WD's attributes) Adding a third form of attributes will confuse users and obfuscate C++ code.

Keywords in Disguise. As stated previously, attributes that aren't ignorable aren't really attributes in the narrow sense. Why not simply add new keywords to C++? This very argument was expressed by many C++ programmers who were exposed to the Working Draft's attributes but it's also expressed by committee members.

Are Attributes Keywords in Disguise?

The latter claim is perhaps the most interesting. Where do you draw the line between an attribute and a full-blown keyword? Clearly, ignorability is no longer a valid criterion for such a distinction because most of the attributes in the Working Draft aren't ignorable. Furthermore, tokens such as [[final]], [[override]] and [[hiding]] exist as true keywords in other programming languages so why sneak them into C++ as attributes? After all, they affect the most fundamental feature of C++ (a class), and other programming languages define them as first class keywords.

In the second part of this series I will present the arguments of attributes supporters and try to predict what will happen to attributes.

Discussions

Bugzilla
Posted Nov 18, 2008 01:53 AM by cupu
2 Replies
auto_ptr issues
Posted Sep 14, 2007 07:43 AM by singh_siddhu
1 Replies
i want c++ book through net
Posted Aug 23, 2007 11:13 PM by harivilu
3 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny KalevNovember 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny KalevOctober 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny KalevOctober 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See More Blogs

Informit Network