473,386 Members | 1,819 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,386 software developers and data experts.

Puzzle: make new compilers understand what g++ 2.95.3 compiled

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 2.95.3 allowed it.

What are the minimal modification, that leave the usage of the class
sound and can be compiled by newer compilers.

Thank you

( compile the following code with s.th. like g++ -g -o Exception
Exception.cpp -lstlport_gcc )

#include "iostream"

class Exception {
public:
Exception(int);
void setErrNo(int i) throw(Exception);
int errNo;

};

Exception::Exception(int e) {
errNo=e;

}

void Exception::setErrNo(int i) throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);

};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtException) {
std::cout << "caught caughtException:" << caughtException.errNo <<
std::endl;
}

May 8 '07 #1
5 1942
On May 8, 1:09 pm, "nospam_n...@wanano.net"
<Torsten.Reich...@wanano.netwrote:
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 2.95.3 allowed it.

What are the minimal modification, that leave the usage of the class
sound and can be compiled by newer compilers.

Thank you

( compile the following code with s.th. like g++ -g -o Exception
Exception.cpp -lstlport_gcc )

#include "iostream"

class Exception {
public:
Exception(int);
void setErrNo(int i) throw(Exception);
int errNo;

};

Exception::Exception(int e) {
errNo=e;

}

void Exception::setErrNo(int i) throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);

};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtException) {
std::cout << "caught caughtException:" << caughtException.errNo <<
std::endl;

}
Remove the `throw(Exception)' statements from the function setErrNo.
Your catch block is trying to catch an exceptions of type Exception
and setErrNo will throw and Exception type so it will be caught
correctly.

Using gcc 3.4.6 it will compile and work fine this way.

The C++ standard states that "Classes in exception specifiers must be
complete types", so throw(Exception) is disallowed.
May 8 '07 #2
Thank you Keith Halligan.

To keep the information in the signatures, we now use an empty
declarative substitute, so the developer can see from the header, what
he might have to catch.

#include "iostream"

#if ( GCC_VERSION 30000 )
# define declare_throw(Exception) throw(Exception)
#else
# define declare_throw(Exception)
#endif

class Exception {
public:
Exception(int);
void setErrNo(int i) declare_throw(Exception);
int errNo;
};

Exception::Exception(int e) {
errNo=e;
}

void Exception::setErrNo(int i) declare_throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);
};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtException) {
std::cout << "caught caughtException:" << caughtException.errNo <<
std::endl;
}
}

May 8 '07 #3
no*********@wanano.net wrote:
Thank you Keith Halligan.

To keep the information in the signatures, we now use an empty
declarative substitute, so the developer can see from the header, what
he might have to catch.

#include "iostream"

#if ( GCC_VERSION 30000 )
# define declare_throw(Exception) throw(Exception)
#else
# define declare_throw(Exception)
#endif

class Exception {
public:
Exception(int);
void setErrNo(int i) declare_throw(Exception);
int errNo;
};

Exception::Exception(int e) {
errNo=e;
}

void Exception::setErrNo(int i) declare_throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);
};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtException) {
std::cout << "caught caughtException:" << caughtException.errNo <<
std::endl;
}
}
This is a good practical solution. Did you try throw(Exception *)? Or is
that something not allowed in C++? I belive you can throw string,
integer etc. So you could throw a string message as well.
May 8 '07 #4
Fei Liu <fe****@aepnetworks.comwrote in news:f1**********@aioe.org:
no*********@wanano.net wrote:
>Thank you Keith Halligan.

To keep the information in the signatures, we now use an empty
declarative substitute, so the developer can see from the header, what
he might have to catch.

#include "iostream"

#if ( GCC_VERSION 30000 )
# define declare_throw(Exception) throw(Exception)
#else
# define declare_throw(Exception)
#endif

class Exception {
public:
Exception(int);
void setErrNo(int i) declare_throw(Exception);
int errNo;
};

Exception::Exception(int e) {
errNo=e;
}

void Exception::setErrNo(int i) declare_throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);
};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtException) {
std::cout << "caught caughtException:" << caughtException.errNo <<
std::endl;
}
}

This is a good practical solution. Did you try throw(Exception *)? Or
is
that something not allowed in C++? I belive you can throw string,
integer etc. So you could throw a string message as well.
Be careful when throwing pointers as exceptions. Specifically, what is
the pointer going to be pointing at by the time the catch clause is
evaluated. If you've thrown a pointer to a local variable, it will be
out of scope by the time the catch clause executes.

May 8 '07 #5
On 8 Maj, 16:41, "nospam_n...@wanano.net"
<Torsten.Reich...@wanano.netwrote:
Thank you Keith Halligan.

To keep the information in the signatures, we now use an empty
declarative substitute, so the developer can see from the header, what
he might have to catch.
I recommend a much better solution: do not use exception
specifications. Instead have the specification in a comment:
void setErrNo(int i); // throws Exception
Exception specifications really are more of less useless and will
often make the code slower and (more important) difficult to maintain/
extend. The comment is harmless here.
I assume that you do not expect the immediate caller to catch the
exception? In that case, using return codes would seem like a better
idea.
Last, I do not understand that usage of "auto". What is it for? The
keyword is clearly superflous here.

/Peter
>
#include "iostream"

#if ( GCC_VERSION 30000 )
# define declare_throw(Exception) throw(Exception)
#else
# define declare_throw(Exception)
#endif

class Exception {
public:
Exception(int);
void setErrNo(int i) declare_throw(Exception);
int errNo;

};

Exception::Exception(int e) {
errNo=e;

}

void Exception::setErrNo(int i) declare_throw(Exception) {
auto Exception methodException(2);
errNo=i;
throw(methodException);

};

int main(char argc, char *argv[], char *env[]) {
try {
auto Exception mainException(1);
mainException.setErrNo(42);
} catch (Exception caughtException) {
std::cout << "caught caughtException:" << caughtException.errNo <<
std::endl;
}

}
May 8 '07 #6

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

Similar topics

58
by: Svein Ove Aas | last post by:
Is anyone working on a python-to-native compiler? I'd be interested in taking a look. Come to think of it, is anyone working on a sexpr-enabled version of Python, or anything similar? I really...
13
by: Derek | last post by:
As I understand it there is a good amount of link compatibility among C compilers. For example, I can compile main.c with GCC and func.c with Sun One and link the objects using either linker (GNU...
1
by: Arpan | last post by:
I am a newbie ASP.NET programmer. While going through the MSDN ASP.NET tutorial, I came across the following sentence: All ASP.NET code is compiled rather than interpreted which allows early...
2
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...
0
by: JosAH | last post by:
Greetings, last week's tip was a bit of playtime where we've built a Sudoku solver. This week we're going to build some complicated stuff: a compiler. Compiler construction is a difficult...
98
by: Tak | last post by:
Some days ago, I written a mini program like this: #include <stdio.h> int main() { char c; int n; scanf("%d",&n); c = getchar();
0
by: JosAH | last post by:
Greetings, welcome back at the sequel of the parsers article chapter. This part is dedicated to the ExpressionParser, the largest parser class for our little language. This class parses a...
1
by: JosAH | last post by:
Greetings, welcome back again. The part of the articles rounds up the discussion on the Interpreter and disusses some code uploaded in the attachment. The Interpreter again The Interpreter...
40
by: castironpi | last post by:
I'm curious about some of the details of the internals of the Python interpreter: I understand C from a hardware perspective. x= y+ 1; Move value from place y into a register Add 1 to the...
14
by: shyam.lingegowda | last post by:
Hi all, If I have two c++ programs and I compile that using two different compilers on Unix. Is it possible for these two exe's to communicate with one another. Since the way the exe's are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.