Xcode / LLVM catch子句与派生类型不匹配

时间:2012-05-15 14:16:57

标签: c++ xcode exception-handling llvm

在gcc 4.2中,这有效:

#include <stdexcept>
#include <iostream>

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

在Xcode 4.3.2(带有LLVM 3.1的iOS,-std = c ++ 11)中,这个失败了terminate called throwing an exception,从未到达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);
}

但这有效:

#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);
}

是什么给出了?

1 个答案:

答案 0 :(得分:2)

gcc是正确的:

  

15.3p3 handler E类型的异常对象的匹配,如果

     
      
  • ...或
  •   
  • 处理程序的类型为 cv T cv T&,而T是明确的公共基类E
  •   
  • ...
  •   

这听起来像是一个xcode错误(而且是一个令人惊讶的基本错误!)