Xcode / LLVM catch clause does not match derived types

In gcc 4.2, this works:

#include <stdexcept>
#include <iostream>

int main() {
    try {
        throw std::runtime_error("abc");
    } catch (const std::exception& ex) {
        std::cout << ex.what();
    }
}

In Xcode 4.3.2 (iOS with LLVM 3.1, -std = C ++ 11) this is not with terminate called throwing an exceptionnever reaching the line NSLog(โ€ฆ):

#include <stdexcept>

int main() {
    try {
        throw std::runtime_error("abc");
    } catch (const std::exception& ex) {
        NSLog(@"%s", ex.what());
    }

    return UIApplicationMain(argc, argv, nil, nil);
}

But it works:

#include <stdexcept>

int main() {
    try {
        throw std::runtime_error("abc");
    } catch (const std::runtime_error& ex) {
        NSLog(@"%s", ex.what());
    }

    return UIApplicationMain(argc, argv, nil, nil);
}

What gives?

+5
source share
1 answer

gcc is correct:

15.3p3 A handler is a match for a type exception object Eif

  • ... or
  • the handler is of type cv Tor cv T&, but it Tis an unambiguous public base class Eor
  • ...

It sounds like an xcode bug (and surprisingly simple!)

+2
source

All Articles