C++98 和 C++11 之间的哪些变化显示出行为差异?
What changes between C++98 and C++11 show difference in behavior?
我正在阅读以下内容post:
- What changes introduced in C++14 can potentially break a program written in C++11?
还有 isocpp 页面:
所以我开始好奇,根据标准:C++11 中引入的哪些更改可能会破坏用 C++98 编写的程序?
突出的大问题 -- 从析构函数中抛出异常。
在 C++98 中,如果您小心的话,您可以拥有执行此操作的程序并且可以正常工作。
在 C++11 中,您通常必须显式声明 dtor noexcept(false)
。
很好 blog post here,在 Andrzej 的 C++ 博客上。
In short, the following program used to run successfully in C++03 (under some definition of “success”):
struct S
{
~S() { throw runtime_error(""); } // bad, but acceptable
};
int main()
{
try { S s; }
catch (...) {
cerr << "exception occurred";
}
cout << "success";
}
In C++11, the same program will trigger the call to std::terminate
.
这是另一个与 C++11 中的析构函数 are noexcept(true) 相关的案例:
// 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;
}
原始参考:
我正在阅读以下内容post:
- What changes introduced in C++14 can potentially break a program written in C++11?
还有 isocpp 页面:
所以我开始好奇,根据标准:C++11 中引入的哪些更改可能会破坏用 C++98 编写的程序?
突出的大问题 -- 从析构函数中抛出异常。
在 C++98 中,如果您小心的话,您可以拥有执行此操作的程序并且可以正常工作。
在 C++11 中,您通常必须显式声明 dtor noexcept(false)
。
很好 blog post here,在 Andrzej 的 C++ 博客上。
In short, the following program used to run successfully in C++03 (under some definition of “success”):
struct S { ~S() { throw runtime_error(""); } // bad, but acceptable }; int main() { try { S s; } catch (...) { cerr << "exception occurred"; } cout << "success"; }
In C++11, the same program will trigger the call to
std::terminate
.
这是另一个与 C++11 中的析构函数 are noexcept(true) 相关的案例:
// 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;
}
原始参考: