std:: bad_alloc
|
Défini dans l'en-tête
<new>
|
||
|
class
bad_alloc
:
public
std::
exception
|
||
std::bad_alloc
est le type de l'objet levé comme exception par les
fonctions d'allocation
pour signaler un échec d'allocation de mémoire.
Diagramme d'héritage
Table des matières |
Fonctions membres
|
(constructeur)
|
construit un nouvel objet
bad_alloc
(fonction membre publique) |
|
operator=
|
remplace l'objet
bad_alloc
(fonction membre publique) |
|
what
|
retourne la chaîne explicative
(fonction membre publique) |
std::bad_alloc:: bad_alloc
| (1) | ||
|
bad_alloc
(
)
throw
(
)
;
|
(jusqu'à C++11) | |
|
bad_alloc
(
)
noexcept
;
|
(depuis C++11)
(constexpr depuis C++26) |
|
| (2) | ||
|
bad_alloc
(
const
bad_alloc
&
other
)
throw
(
)
;
|
(jusqu'à C++11) | |
|
bad_alloc
(
const
bad_alloc
&
other
)
noexcept
;
|
(depuis C++11)
(constexpr depuis C++26) |
|
Construit un nouvel objet
bad_alloc
avec une chaîne d'octets terminée par un caractère nul définie par l'implémentation, accessible via
what()
.
std::bad_alloc
alors
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
.
(depuis C++11)
Paramètres
| other | - | un autre objet exception à copier |
std::bad_alloc:: operator=
|
bad_alloc
&
operator
=
(
const
bad_alloc
&
other
)
throw
(
)
;
|
(jusqu'à C++11) | |
|
bad_alloc
&
operator
=
(
const
bad_alloc
&
other
)
noexcept
;
|
(depuis C++11)
(constexpr depuis C++26) |
|
Assigne le contenu avec celui de
other
.
Si
*
this
et
other
ont tous deux le type dynamique
std::bad_alloc
alors
std::
strcmp
(
what
(
)
, other.
what
(
)
)
==
0
après l'assignation.
(depuis C++11)
Paramètres
| other | - | un autre objet d'exception à assigner |
Valeur de retour
* this
std::bad_alloc:: what
|
virtual
const
char
*
what
(
)
const
throw
(
)
;
|
(jusqu'à C++11) | |
|
virtual
const
char
*
what
(
)
const
noexcept
;
|
(depuis C++11)
(constexpr depuis C++26) |
|
Retourne la chaîne explicative.
Valeur de retour
Pointeur vers une chaîne terminée par un caractère nul, définie par l'implémentation, contenant des informations explicatives. La chaîne est adaptée à la conversion et à l'affichage en tant que std::wstring . Le pointeur est garanti d'être valide au moins jusqu'à la destruction de l'objet exception duquel il est obtenu, ou jusqu'à l'appel d'une fonction membre non constante (par exemple, l'opérateur d'affectation par copie) sur l'objet exception.
|
La chaîne retournée est encodée avec l'encodage littéral ordinaire pendant l'évaluation constante. |
(depuis C++26) |
Notes
Les implémentations sont autorisées mais non obligées de redéfinir
what()
.
Hérité de std:: exception
Fonctions membres
|
[virtuel]
|
détruit l'objet exception
(fonction membre publique virtuelle de
std::exception
)
|
|
[virtuel]
|
retourne une chaîne explicative
(fonction membre publique virtuelle de
std::exception
)
|
Notes
| Macro de test de fonctionnalité | Valeur | Std | Fonctionnalité |
|---|---|---|---|
__cpp_lib_constexpr_exceptions
|
202411L
|
(C++26) | constexpr pour les types d'exception |
Exemple
#include <iostream> #include <new> int main() { try { while (true) { new int[100000000ul]; } } catch (const std::bad_alloc& e) { std::cout << "Allocation failed: " << e.what() << '\n'; } }
Sortie possible :
Allocation failed: std::bad_alloc
Voir aussi
|
fonctions d'allocation
(fonction) |