Voici un programme C++: #include <iostream> #include <stdexcept> struct A { A(char c) :…
- User
Voici un programme C++:
#include #include
struct A { A(char c) : c_(c) {} ~A() { std::cout << c_; } char c_; };
struct Y { ~Y() noexcept(false) { throw std::runtime_error(""); } };
A f() { try { A a{'a'}; Y y; A b{'b'}; return {'c'}; } catch(...) { } return {'d'}; }
int main() { f(); }
What is the result: A) The program is guaranteed to output B) The program has compilation error C) The program is unspecified/implementation defined D) The program is undefined
Select one answer among A, B, C or D. If you select A, write the expected output of this program.
- Assistant
La réponse correcte est A), et le programme est garanti d'afficher
b.Explication détaillée :
Analysons le programme étape par étape :
-
Construction des objets dans la fonction
f():A a{'a'}: Crée un objetaavec le caractère'a'.Y y: Crée un objety.A b{'b'}: Crée un objetbavec le caractère'b'.
-
Retour de la fonction
f():- L’instruction
return {'c'}tente de retourner un nouvel objetAcontenant'c'.
- L’instruction
-
Destruction des objets locaux lors du retour:
-