在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);
}
是什么给出了?
答案 0 :(得分:2)
gcc是正确的:
15.3p3 handler 是
E
类型的异常对象的匹配,如果
- ...或
- 处理程序的类型为 cv
T
或 cvT&
,而T
是明确的公共基类E
或- ...
这听起来像是一个xcode错误(而且是一个令人惊讶的基本错误!)