Namespaces
Variants

std::is_within_lifetime

Von de.cppreference.net
 
 
Dienstprogrammbibliothek
Sprachunterstützung
Typunterstützung (Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Koroutinenunterstützung (C++20)
Vertragsunterstützung (C++26)
Dreiseitiger Vergleich
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

Allgemeine Dienstprogramme
Relationsoperatoren (veraltet in C++20)
Ganzzahl-Vergleichsfunktionen
(C++20)(C++20)(C++20)    
(C++20)
Tausch und Typoperationen
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Allgemeine Vokabular-Typen
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
Definiert in Header <type_traits>
template< class T >
consteval bool is_within_lifetime( const T* ptr ) noexcept;
(seit C++26)

Bestimmt, ob der Zeiger ptr auf ein Objekt zeigt, das sich in seiner Lebensdauer befindet.

Während der Auswertung eines Ausdrucks E als Kernkonstantausdruck ist ein Aufruf von std::is_within_lifetime fehlerhaft, es sei denn, ptr zeigt auf ein Objekt,

Parameter

p - Zeiger zur Erkennung

Rückgabewert

true falls der Zeiger ptr auf ein Objekt zeigt, das sich innerhalb seiner Lebensdauer befindet; andernfalls false .

Hinweise

Feature-Test Makro Wert Std Funktion
__cpp_lib_is_within_lifetime 202306L (C++26) Überprüfung, ob ein Union-Alternativ aktiv ist

Beispiel

std::is_within_lifetime kann verwendet werden, um zu prüfen, ob ein Union-Member aktiv ist:

#include <type_traits>
// an optional boolean type occupying only one byte,
// assuming sizeof(bool) == sizeof(char)
struct optional_bool
{
    union { bool b; char c; };
    // assuming the value representations for true and false
    // are distinct from the value representation for 2
    constexpr optional_bool() : c(2) {}
    constexpr optional_bool(bool b) : b(b) {}
    constexpr auto has_value() const -> bool
    {
        if consteval
        {
            return std::is_within_lifetime(&b); // during constant evaluation,
                                                // cannot read from c
        }
        else
        {
            return c != 2; // during runtime, must read from c
        }
    }
    constexpr auto operator*() -> bool&
    {
        return b;
    }
};
int main()
{
    constexpr optional_bool disengaged;
    constexpr optional_bool engaged(true);
    static_assert(!disengaged.has_value());
    static_assert(engaged.has_value());
    static_assert(*engaged);
}

Siehe auch

prüft, ob ein Typ ein impliziter Lebensdauertyp ist
(Klassenschablone)