std::integral_constant
Von cppreference.com
| Definiert in der Kopfdatei <type_traits> |
||
| template< class T, T v > struct integral_constant; |
(seit C++11) | |
std::integral_constant umschließt eine statische Konstante des angegebenen Typs. Es ist die Basisklasse für die C++ Type Traits.
Wenn das Programm Spezialisierungen für std::integral_constant hinzufügt, ist das Verhalten undefiniert.
Inhalt |
[bearbeiten] Hilfs-Alias-Templates
Ein Hilfs-Alias-Template std::bool_constant ist für den häufigen Fall definiert, dass T bool ist.
| template< bool B > using bool_constant = integral_constant<bool, B>; |
(seit C++17) | |
[bearbeiten] Spezialisierungen
Zwei Typedefs für den häufigen Fall, dass T bool ist, werden bereitgestellt
| Definiert in der Kopfdatei
<type_traits> | |
| Name | Definition |
true_type
|
std::integral_constant<bool, true> |
false_type
|
std::integral_constant<bool, false> |
[bearbeiten] Mitgliedstypen
| Name | Definition |
value_type
|
T |
type
|
std::integral_constant<T, v> |
[bearbeiten] Mitgliedskonstanten
| Name | Wert |
| constexpr T value [static] |
v (öffentliche statische Member-Konstante) |
[bearbeiten] Memberfunktionen
| operator value_type |
gibt den umschlossenen Wert zurück (public member function) |
| operator() (C++14) |
gibt den umschlossenen Wert zurück (public member function) |
std::integral_constant::operator value_type
| constexpr operator value_type() const noexcept; |
||
Konvertierungsfunktion. Gibt den umschlossenen Wert zurück.
std::integral_constant::operator()
| constexpr value_type operator()() const noexcept; |
(seit C++14) | |
Gibt den umschlossenen Wert zurück. Diese Funktion ermöglicht es std::integral_constant, als Quelle für Compile-Time Function Objects zu dienen.
[bearbeiten] Mögliche Implementierung
template<class T, T v> struct integral_constant { static constexpr T value = v; using value_type = T; using type = integral_constant; // using injected-class-name constexpr operator value_type() const noexcept { return value; } constexpr value_type operator()() const noexcept { return value; } // since c++14 }; |
[bearbeiten] Hinweise
| Feature-Test-Makro | Wert | Std | Feature |
|---|---|---|---|
__cpp_lib_integral_constant_callable |
201304L |
(C++14) | std::integral_constant::operator()
|
__cpp_lib_bool_constant |
201505L |
(C++17) | std::bool_constant
|
[bearbeiten] Beispiel
Führen Sie diesen Code aus
#include <type_traits> using two_t = std::integral_constant<int, 2>; using four_t = std::integral_constant<int, 4>; static_assert(not std::is_same_v<two_t, four_t>); static_assert(two_t::value * 2 == four_t::value, "2*2 != 4"); static_assert(two_t() << 1 == four_t() >> 0, "2*2 != 4"); enum class E{ e1, e2 }; using c1 = std::integral_constant<E, E::e1>; using c2 = std::integral_constant<E, E::e2>; static_assert(c1::value != E::e2); static_assert(c1() == E::e1); static_assert(std::is_same_v<c2, c2>); int main() {}
[bearbeiten] Siehe auch
| (C++14) |
implementiert eine Compile-time-Sequenz von ganzen Zahlen (Klassenvorlage) |