2
A
回答
2
突出的一个 - 从析构函数中抛出异常。
在C++ 98中,如果你小心的话,你可以使用这样的程序并且工作正常。
在C++ 11中,您经常需要明确声明dtor noexcept(false)
。
不错blog post here,在Andrzej的C++博客上。
简而言之,使用了以下的方案,以在C++ 03成功运行(下的“成功”一些定义):
struct S { ~S() { throw runtime_error(""); } // bad, but acceptable }; int main() { try { S s; } catch (...) { cerr << "exception occurred"; } cout << "success"; }
在C++ 11中,相同的程序将触发致电
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;
}
原始参考:
相关问题
- 1. C++ 11:T和T&推导[行为差异,两者之间] decltype行为
- 2. C和C++之间的链接差异?
- 3. c#和java之间的差异写()
- 4. }之间的差异;和}在C++
- 5. JAVA和C#之间的LdapConnection差异
- 6. C++:0和0.0之间的差异?
- 7. C#和java之间的输出差异
- 8. C mmap和numpy memmap之间的差异
- 9. C#和Java之间的Base64差异
- 10. C中#pragma和_Pragma()之间的差异
- 11. C#,物业之间的差异变和不变量
- 12. 差异在C和Java之间链表
- 13. SQL:行之间的差异
- 14. C和C++中的类之间的差异
- 15. C++ 11命名空间查找从C++ 98更改?
- 16. C++ 11 STD阵列 - gcc和视觉工作室之间的差异
- 17. C和C++之间的细微差别
- 18. 结构在C和C++之间的差异
- 19. C#MD5和Objective C之间的差异MD5
- 20. Visual C#(Visual Studio)和Mono C#GUI之间的差异
- 21. 初始化指针,C和C之间的差异++编译器(GCC-4.3.4)
- 22. C#倒计时(sql getdata()和datetime.now之间的差异)没有显示
- 23. contains和containstable之间的显着差异?
- 24. 地址之间的C++差异
- 25. C++编译器之间的差异
- 26. C#铸造类型之间的差异
- 27. 现在和现在之间的差异时间抛出1小时差异(C++)
- 28. 的boost ::变异和多态性C++ 11
- 29. WPF和WinForms WebBrowser控件之间存在哪些功能差异?
- 30. 将代码从C++ 11转换为C++ 98?
一些有趣的情况在这里:https://stackoverflow.com/questions/6399615/what-breaking-changes-are-introduced-in-c11我以为我有一个很好的例子,但它基于* implicit int *,它从未正式转入C++。 – Bathsheba