◐ Shell
clean mode source ↗

Static Assertion - cppreference.com

Da cppreference.com.

Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.

La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui.

Click here for the English version of this page

<metanoindex/>

Esegue in fase di compilazione affermazione

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.

Sintassi

static_assert ( bool_constexpr , string ) (dal C++11)

Spiegazione

  • bool_constexpr una espressione booleana costante da valutare

    Original:

    bool_constexpr a boolean constant expression to be evaluated

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • string stringa letterale che apparirà come errore di compilazione se bool_constexpr è falsa

    Original:

    string string literal that will appear as compiler error if bool_constexpr is false

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Esempio

#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
}

Possible output:

1: error: static assertion failed: Swap requires copying
2: error: static assertion failed: Data Structure requires default-constructible elements

Vedi anche