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

2024-04-05


437. Is type of class allowed in member function exception specification?

Section: 11.4  [class.mem]     Status: CD1     Submitter: Cary Coutant     Date: 10 Oct 2003

[Voted into WP at April 2005 meeting.]

I've encountered a C++ program in which a member function wants to declare that it may throw an object of its own class, e.g.:

  class Foo {
  private:
     int val;
  public:
     Foo( int &initval ) { val = initval; };
     void throwit() throw(Foo) { throw (*this); };
  };

The compiler is complaining that Foo is an incomplete type, and can't be used in the exception specification.

My reading of the standard [basic.types] is inconclusive. Although it does state that the class declaration is considered complete when the closing brace is read, I believe it also intends that the member function declarations should not be semantically validated until the class has been completely declared.

If this isn't allowed, I don't know how else a member function could be declared to throw an object of its own class.

John Spicer: The type is considered complete within function bodies, but not in their declaration (see 11.4 [class.mem] paragraph 2).

Proposed Resolution:

Change 11.4 [class.mem] paragraph 2 as follows:

Within the class member-specification, the class is regarded as complete within function bodies, default arguments, exception-specifications, and constructor ctor-initializers (including such things in nested classes).

Rationale: Taken with 9.3.4.6 [dcl.fct] paragraph 6, the exception-specification is the only part of a function declaration/definition in which the class name cannot be used because of its putative incompleteness. There is no justification for singling out exception specifications this way; both in the function body and in a catch clause, the class type will be complete, so there is no harm in allowing the class name to be used in the exception-specification.