Exception Specifications Using Undefined Class

  • Thread starter Scott Brady Drummonds
  • Start date
S

Scott Brady Drummonds

A coworker of mine tried compiling the following code in two versions of the
same compiler:

<quote>
class A;

class B {
public:
int b;
int printSomething () throw (A);
};

class A {
int a;
};


int main() {

B bb;
return 0;
}
</quote>

In one, the code compiled. In the newer version of the same compiler (GCC,
FWIW) it complained:

<quote>
test.cpp:6: invalid use of undefined type `struct A'
test.cpp:1: forward declaration of `struct A'
</quote>

We were talking about the reason for the failure and unable to come to a
consensus as to why the newer version was complaining. As far as I
understand it, I don't know why any compiler would compile this code. It
would seem to me that the exception specification for 'printSomething' would
have to know the size of the variable passed as its parameter. Since that
variable's type, A, has been declared but not defined, the size is unknown.
So, in my estimation, the newer version is correct in erroring and the older
one was likely sloppy in allowing compilation.

What are your thoughts?

Scott
 
D

Denis Remezov

Scott said:
A coworker of mine tried compiling the following code in two versions of the
same compiler:

<quote>
class A;

class B {
public:
int b;
int printSomething () throw (A);
};

class A {
int a;
};

int main() {

B bb;
return 0;
}
</quote>

In one, the code compiled. In the newer version of the same compiler (GCC,
FWIW) it complained:

<quote>
test.cpp:6: invalid use of undefined type `struct A'
test.cpp:1: forward declaration of `struct A'
</quote>

We were talking about the reason for the failure and unable to come to a
consensus as to why the newer version was complaining. As far as I
understand it, I don't know why any compiler would compile this code. It
would seem to me that the exception specification for 'printSomething' would
have to know the size of the variable passed as its parameter. Since that
variable's type, A, has been declared but not defined, the size is unknown.
So, in my estimation, the newer version is correct in erroring and the older
one was likely sloppy in allowing compilation.

The newer version is correct: incomplete types are not allowed in an
exception specification (declaration or definition), and neither are references
or pointers to incomplete types (15.4/1) (though [cv] void* are allowed).
The same restrictions apply to the throw expression and the catch clause.

I don't know about the rationale for the above in regards to a function
/declaration/ (not a definition or a function call).

Denis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top