2017-10-09 30 views
2

私は以下の記事を読んでいた。C++ 98とC++ 11の違いは動作の違いを示していますか?

ともisocppページ:

だから私は標準によると、好奇心旺盛になりましたC++ 11で導入された変更により、 C++ 98で書かれたプログラムですか?

+0

ここではいくつかの興味深い例:https://stackoverflow.com/questions/6399615/what-breaking-changes-are-introduced-in-c11私は良い例があると思ったが、 C++に正式に引き継がれなかった*暗黙のint *に基づいていました。 – Bathsheba

答えて

2

顕著なもの - デストラクタから例外をスローします。

C++ 98では、注意深い場合にはこれを行い、正常に動作するプログラムを作成することができます。

C++ 11では、多くの場合、明示的にdtorを宣言する必要がありますnoexcept(false)

ニースblog post here、AndrzejのC++ブログにあります。 C++ 03(「成功」のいくつかの定義下)で正常に実行するために使用される短い、以下のプログラムで

:C++ 11では

struct S 
{ 
    ~S() { throw runtime_error(""); } // bad, but acceptable 
}; 

int main() 
{ 
    try { S s; } 
    catch (...) { 
    cerr << "exception occurred"; 
    } 
    cout << "success"; 
} 

を、同じプログラムがトリガされますstd::terminateに電話してください。ここで

0

はデストラクタに関連する別のケースであるC++ 11で(真)noexceptです:

// A simple program that demonstrates how C++11 and pthread_cancel don't play 
// nicely together. 
// 
// If you build without C++11 support (g++ threadkill.cpp -lpthread), the 
// application will work as expected. After 5 seconds, main() will cancel the 
// thread it created and the program will successfully exit. 
// 
// If you build with C++11 support(g++ -std=c++11 threadkill.cpp -lpthread), 
// the program will crash because the abi::__forced_unwind exception will 
// escape the destructor, which is implicitly marked as noexcept(true) in 
// C++11. If you mark the destructor as noexcept(false), the program does 
// not crash. 
#include <iostream> 
#include <unistd.h> 
#include <string.h> 

class sleepyDestructorObject 
{ 
public: 
    ~sleepyDestructorObject() //noexcept(false) 
    { 
     std::cout << "sleepy destructor invoked" << std::endl; 
     while(true) 
     { 
      std::cout << "." << std::flush; 
      sleep(1); 
     } 
    } 
}; 

void* threadFunc(void* lpParam) 
{ 
    sleepyDestructorObject sleepy; 
    return NULL; 
} 

int main(int argc, char** argv) 
{ 
    pthread_t tThreadID; 
    pthread_create(&tThreadID, NULL, threadFunc, NULL); 
    sleep(5); 
    pthread_cancel(tThreadID); 
    pthread_join(tThreadID, NULL); 
    return 0; 
} 

オリジナル参照:

関連する問題