Namespaces
Variants

std:: is_const

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
is_const
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
Definiert im Header <type_traits>
template < class T >
struct is_const ;
(seit C++11)

std::is_const ist ein UnaryTypeTrait .

Falls T ein const-qualifizierter Typ ist (also const oder const volatile ), liefert die Memberkonstante value den Wert true . Für jeden anderen Typ ist value gleich false .

Wenn das Programm Spezialisierungen für std::is_const oder std::is_const_v hinzufügt, ist das Verhalten undefiniert.

Inhaltsverzeichnis

Template-Parameter

T - ein zu prüfender Typ

Hilfsvariablen-Template

template < class T >
constexpr bool is_const_v = is_const < T > :: value ;
(seit C++17)

Geerbt von std:: integral_constant

Member-Konstanten

value
[static]
true wenn T ein const-qualifizierter Typ ist, false andernfalls
(öffentliche statische Member-Konstante)

Member-Funktionen

operator bool
konvertiert das Objekt zu bool , gibt value zurück
(öffentliche Member-Funktion)
operator()
(C++14)
gibt value zurück
(öffentliche Member-Funktion)

Member-Typen

Typ Definition
value_type bool
type std:: integral_constant < bool , value >

Hinweise

Wenn T ein Referenztyp ist, dann ist is_const < T > :: value immer false . Der korrekte Weg, um einen potenziellen Referenztyp auf Konstanz zu prüfen, ist das Entfernen der Referenz: is_const < typename remove_reference < T > :: type > .

Mögliche Implementierung

template<class T> struct is_const          : std::false_type {};
template<class T> struct is_const<const T> : std::true_type {};

Beispiel

#include <type_traits>
static_assert(std::is_same_v<const int*, int const*>,
    "Denken Sie daran: Constness bindet innerhalb von Zeigern stark.");
static_assert(!std::is_const_v<int>);
static_assert(std::is_const_v<const int>);
static_assert(!std::is_const_v<int*>);
static_assert(std::is_const_v<int* const>,
    "Weil der Zeiger selbst nicht geändert werden kann, aber das referenzierte int.");
static_assert(!std::is_const_v<const int*>,
    "Weil der Zeiger selbst geändert werden kann, aber nicht das referenzierte int.");
static_assert(!std::is_const_v<const int&>);
static_assert(std::is_const_v<std::remove_reference_t<const int&>>);
struct S
{
    void foo() const {}
    void bar() const {}
};
int main()
{
    // Eine const-Memberfunktion ist auf andere Weise const:
    static_assert(!std::is_const_v<decltype(&S::foo)>,
        "Weil &S::foo ein Zeiger ist.");
    using S_mem_fun_ptr = void(S::*)() const;
    S_mem_fun_ptr sfp = &S::foo;
    sfp = &S::bar; // OK, kann neu ausgerichtet werden
    static_assert(!std::is_const_v<decltype(sfp)>,
        "Weil sfp der gleiche Zeigertyp ist und somit neu ausgerichtet werden kann.");
    const S_mem_fun_ptr csfp = &S::foo;
    // csfp = &S::bar; // Fehler
    static_assert(std::is_const_v<decltype(csfp)>,
        "Weil csfp nicht neu ausgerichtet werden kann.");
}

Siehe auch

prüft, ob ein Typ volatile-qualifiziert ist
(Klassentemplate)
(C++17)
erhält eine Referenz auf const zu seinem Argument
(Funktionstemplate)