• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Problem with dynamic_cast & exceptions using GCC 4?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem with dynamic_cast & exceptions using GCC 4?


  • Subject: Problem with dynamic_cast & exceptions using GCC 4?
  • From: Jesper Papmehl <email@hidden>
  • Date: Mon, 25 Jul 2005 19:07:48 +0200

Hi!

I am have trouble getting dynamic_cast to do the right thing when compiling with GCC 4.

We use a hierarchy of exceptions (mainly the standard ones in stdexcept) and in various places in out code, we use dynamic_cast to convert between exceptions and error codes. We do this by catch:ing std::exception references, and then try to dynamic_cast the caught exception to the various exception classes to see which one it is. This works fine in CodeWarrior and Visual Studio, and also in Xcode when using GCC 3.3, but not when using GCC 4.

For example, when running the following program:

#include <iostream>
#include <stdexcept>

int main(int argc, char* const argv[]) {
    try {
        throw std::invalid_argument("");
    }
    catch (const std::exception& e) {
        if (dynamic_cast<const std::invalid_argument*>(&e) != NULL) {
            std::cout << "got invalid_argument" << std::endl;
        }
        else if (dynamic_cast<const std::logic_error*>(&e) != NULL) {
            std::cout << "got logic_error" << std::endl;
        }
        else {
            std::cout << "got something else" << std::endl;
        }
    }

    return 0;
}

I get a logic_error instead of an invalid_argument when using GCC 4.

I am using Xcode 2.1 and a freshly created C++ Tool project with the default settings. (C++ exceptions and RTTI are turned on.)
If I change the compiler version from GCC 4 to GCC 3.3, I get an invalid_argument instead, which is what I expect.



Does anyone have any idea what is going on? Do I have the wrong expectation about how this is supposed to work? Or is it maybe a GCC bug?



TIA

/Jesper Papmehl

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Problem with dynamic_cast & exceptions using GCC 4?
      • From: Andreas Grosam <email@hidden>
    • Re: Problem with dynamic_cast & exceptions using GCC 4?
      • From: Olivier Tristan <email@hidden>
  • Prev by Date: Re: Xcode copies files when it adds them (was Re: Please help - Carbon Application problem)
  • Next by Date: Re: Invalid PCH file warning?
  • Previous by thread: Re: Invalid PCH file warning?
  • Next by thread: Re: Problem with dynamic_cast & exceptions using GCC 4?
  • Index(es):
    • Date
    • Thread