This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH: Implement __attribute__((final)) for C++, restricting subtyping and overriding

On Fri, Oct 10, 2008 at 1:04 PM, Doug Gregor <> wrote:
> On Fri, Oct 10, 2008 at 1:53 PM, Jeffrey Yasskin <> wrote:
>> Hi all.
>> I've implemented __attribute__ ((final)) for C++ classes and virtual
>> member functions. It behaves very similarly to Java's final keyword:
>> when applied to a class, it prevents anyone from subtyping that class;
>> and when applied to a virtual member function, it prevents any
>> subtypes from overriding that function. The patch shouldn't affect any
>> code that doesn't use the attribute. In theory, knowing that a method
>> is the final override could allow g++ to, in some cases, emit static
>> calls instead of virtual calls, but I haven't implemented that here.
> Does this implement exactly the behavior required for the "final"
> attribute that just went into C++0x?

I think it implements the same behavior on virtual functions, but may
be different on classes. In particular, the draft standard says, "If
the attribute is speciïed for a class definition, it is equivalent to
being speciïed for each virtual member function of that class,
including inherited member functions." To me, that says that the
following code would be legal:

class Base [[final]] {
  void foo();
  virtual void bar();

class Derived : public Base {
  // Just don't override bar(), and you're fine.

My patch would complain about deriving any class from a final class,
even if the final class had no virtual functions. I think my behavior
makes more sense -- enough more that the definition in the draft
standard looks like a mistake -- but if you don't think it's likely to
change I'll change my patch to implement that instead.

The location of the attribute also seems to be different, since gcc
wants me to write "class __attribute__((final)) Base", and the draft
standard wants me to write "class Base [[final]]". I could be
misunderstanding that, and it may naturally fix itself as C++0X
attributes get implemented.

Thanks for taking a look,

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]