Exception Specifications Using Undefined Class



 DEVELOP > c-Plus-Plus > Exception Specifications Using Undefined Class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Scott Brady Drummonds"
Date: 29 Jul 2004 05:03:54 PM
Object: Exception Specifications Using Undefined Class
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
--
Remove .nospam from my e-mail address to mail me.
.

User: "Denis Remezov"

Title: Re: Exception Specifications Using Undefined Class 29 Jul 2004 05:35:35 PM
Scott Brady Drummonds wrote:


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
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER