Static Assertion
De cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
<metanoindex/>
Effectue la vérification d'assertion moment de la compilation
Original:
Performs compile-time assertion checking
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Syntaxe
static_assert ( bool_constexpr , string )
|
(depuis C++11) | ||||||||
Explication
- bool_constexpr une expression booléenne constante à évaluerOriginal:bool_constexpr a boolean constant expression to be evaluatedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - string chaîne littérale qui apparaît comme une erreur du compilateur si bool_constexpr est fausseOriginal:string string literal that will appear as compiler error if bool_constexpr is falseThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Exemple
#include <type_traits>
template<class T>
void swap( T& a, T& b)
{
static_assert(std::is_copy_constructible<T>::value,
"Swap requires copying");
auto c = b;
b = a;
a = c;
}
template<class T>
struct data_structure
{
static_assert(std::is_default_constructible<T>::value,
"Data Structure requires default-constructible elements");
};
struct no_copy
{
no_copy ( const no_copy& ) = delete;
no_copy () = default;
};
struct no_default
{
no_default ( ) = delete;
};
int main()
{
int a, b;
swap(a,b);
no_copy nc_a, nc_b;
swap(nc_a,nc_b); // 1
data_structure<int> ds_ok;
data_structure<no_default> ds_error; // 2
}
Résultat possible :
1: error: static assertion failed: Swap requires copying
2: error: static assertion failed: Data Structure requires default-constructible elements