Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode/LLVM catch clause not matching 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 fails with terminate called throwing an exception, never reaching the NSLog(…) line:

#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 this 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?

like image 629
Marcelo Cantos Avatar asked Nov 05 '22 00:11

Marcelo Cantos


1 Answers

gcc is correct:

15.3p3 A handler is a match for an exception object of type E if

  • ... or
  • the handler is of type cv T or cv T& and T is an unambiguous public base class of E, or
  • ...

This sounds like an xcode bug (and a surprisingly basic one!)

like image 117
aschepler Avatar answered Nov 10 '22 20:11

aschepler