GCC MSVC ICC 某些情況下 virtual base 解構兩次

作者: nh60211as   2021-12-17 21:25:51
https://old.reddit.com/r/cpp/comments/rg882y
Reddit 看到ㄉ
下面這段程式碼會讓空的 base strct A 解構兩次
GCC, MSVC, ICC 都有這個 bug
貼文的人說 Clang 沒有,可是有人也讓 Clang 產出錯誤結果
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103711#c2
01. #include <iostream>
02.
03. int constructions = 0;
04. int destructions = 0;
05.
06. struct A
07. {
08. A()
09. {
10. constructions++;
11. }
12. virtual ~A() {
13. destructions++;
14. }
15. };
16.
17. struct B : public virtual A
18. {
19. B(int dummy)
20. {
21. };
22.
23. B() : B(1)
24. {
25. throw -1;
26. }
27. virtual ~B() = default;
28. };
29.
30. struct C : public B
31. {
32. };
33.
34. int main() {
35. try
36. {
37. C c;
38. }
39. catch (int e)
40. {
41. std::cout << "Caught: " << e << std::endl;
42. }
43. std::cout << constructions << " constructions" << std::endl;
44. std::cout << destructions << " destructions" << std::endl;
45. return 0;
46. }
作者: ZooseWu (N5)   2021-12-17 21:26:00
看不懂 哭了
作者: wwndbk (黑人問號)   2021-12-17 21:27:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com