2012-05-15 46 views
5

在GCC 4.2的Xcode/LLVM catch子句,這工作:不匹配的派生類型

#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,-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

取出'const'並告訴我們你得到了什麼。 –

+0

@MarkRansom:沒有變化。我試過'... catch(std :: exception ex)...';再次,沒有改變。 –

回答

2

GCC是正確的:

15.3p3甲處理機E類型的異常對象,如果

  • ...或
  • 處理機是一個匹配類型cvTcvT&T是一個明確的公共基類的E,或
  • ...

這聽起來像一個Xcode的bug(和一個令人驚訝的基本的一個!)

+0

感謝編輯@Heatsink。所以你實際上可以在報價單中列出一個列表,現在我想我看到了。 – aschepler