473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.

Jul 22 '05 #1
1 1456
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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: cody | last post by:
public DateTime Value { get { try { return new DateTime(int.Parse(tbYear.Text), int.Parse(tbMonth.Text), int.Parse(tbDay.Text)); } catch (FormatException)
2
by: Stefan Pantos | last post by:
Dear all, Could someone explain to me the proper use of throw()? As it is used for std::exception. I cannot find anything which describes how it should be used and all the information about...
10
by: linq936 | last post by:
Hi, I have many assert() call in my code, now I am considering to replace them with exception. The reason I want to do this change is that with the program going bigger and bigger, it is hard to...
6
by: benben | last post by:
Why doesn't the following code work? template <typename ExceptionT> void f(void) throw (ExceptionT) { throw ExceptionT(); } Ben
41
by: Stuart Golodetz | last post by:
Hi all, Just wondering whether there's any reason why exception specifications are enforced at runtime, rather than at compile-time like in Java? (This was prompted by reading an article on...
5
by: nospam_news | last post by:
When language changes make old code uncompilable, that's not what is called protection of investment. New compilers (g++ 3.2.3) reject classes where methods throw the class they belong to. gcc...
11
by: George2 | last post by:
Hello everyone, How do you understand the Bjarne's comments about exception specification? Especially, "not required to be checked across compilation-unit" and "violations will not be caught...
12
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
4
by: amphetaman | last post by:
If I derive my own Exception class from std::runtime_error, do I have to write the destructor even if its body is empty? #include <stdexcept> class Exception : public std::runtime_error {...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.