This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 115e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-11-11


817. Meaning of [[final]] applied to a class definition

Section: _N3225_.7.6.4  [dcl.attr.final]     Status: CD2     Submitter: US     Date: 3 March, 2009

N2800 comment US 42

[Voted into WP at March, 2010 meeting.]

According to _N3225_.7.6.4 [dcl.attr.final] paragraph 1, the [[final]] attribute applied to a class is just a shorthand notation for marking each of the class's virtual functions as [[final]]. This is different from the similar usage in other languages, where it means that the class so marked cannot be used as a base class. This discrepancy is confusing, and the definition used by the other languages is more useful.

Notes from the March, 2009 meeting:

The intent of the [[final]] attribute is as an aid in optimization, to avoid virtual function calls when the final overrider is known. It is possible to use the [[final]] attribute to prevent derivation by marking the destructor as [[final]]; in fact, as most polymorphic classes will, as a matter of good programming practice, have a virtual destructor, marking the class as [[final]] will have the effect of preventing derivation.

Nonetheless, the general consensus of the CWG was to change the meaning of class [[final]] to parallel the usage in other languages.

Proposed resolution (October, 2009):

  1. Change _N3225_.7.6.4 [dcl.attr.final] paragraph 1 and add a new paragraph, as follows:

  2. The attribute-token final specifies derivation semantics for a class and overriding semantics for a virtual function. It shall appear at most once in each attribute-list and no attribute-argument-clause shall be present. The attribute applies to class definitions and to virtual member functions being declared in a class definition. If the attribute is specified for a class definition, it is equivalent to being specified for each virtual member function of that class, including inherited member functions.

    If some class B is marked final and a class D is derived from B the program is ill-formed.

  3. Change the example in _N3225_.7.6.4 [dcl.attr.final] paragraph 3 as follows:

  4.   struct B1 {
        virtual void f [[ final ]] ();
      };
    
      struct D1 : B1 {
        void f();          // ill-formed
      };
    
      struct [[ final ]] B2 {
      };
    
      struct D2 : B2 {    // ill-formed
      };