type traits library
Megjelenés
Főnév
type traits library (tsz. type traits libraries)
- (informatika) A Type Traits Library a C++ standard könyvtár része (
<type_traits>fejléccel), amely típusokról ad információt fordítási időben, és lehetővé teszi, hogy sablonokat típusfüggően módosítsunk. Ez a könyvtár a template metaprogramming egyik alapköve.
📦 Hol található?
#include <type_traits>
🎯 Mire jó a type traits?
- Típusellenőrzés: Megállapítható, hogy egy típus
int,float,pointer,class, stb. - Típusmódosítás: Eltávolítható
const, referencia, pointer stb. - Sablonkorlátozás (
enable_if): Csak bizonyos típusokra engedjük egy sablon instanciálását. - Fordításkori döntés: Kód elágaztatása
constexpr ifvagystd::conditionalalapján.
🧠 Használat: értékfüggő traits
std::is_integral<T>::value
std::is_integral<int>::value // true
std::is_integral<double>::value // false
std::is_pointer<T>::value
std::is_pointer<int*>::value // true
std::is_pointer<int>::value // false
std::is_same<T1, T2>::value
std::is_same<int, int>::value // true
std::is_same<int, const int>::value // false
🧰 Típusmódosító traits
std::remove_const<T>::type
using T = const int;
using U = std::remove_const<T>::type; // U == int
std::remove_reference<T>::type
int& ref = x;
std::remove_reference<decltype(ref)>::type // int
std::decay<T>::type
Olyan típusra alakít, mint amit egy függvényparaméter automatikusan felvenne:
std::decay<int&>::type // int
std::decay<int[]>::type // int*
🔄 Elágazás sablonban: std::conditional
template<bool B>
using int_or_double = typename std::conditional<B, int, double>::type;
int_or_double<true> x = 5; // int
int_or_double<false> y = 3.14; // double
🔒 Típuskorlátozás: std::enable_if
template<typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
dupla(T x) {
return x * 2;
}
Alternatíva C++20-tól: requires és koncepciók (concepts).
⚙️ Gyakori type_traits típusok
| Név | Leírás |
|---|---|
std::is_void<T> |
T void típus-e |
std::is_array<T> |
T tömb-e |
std::is_pointer<T> |
T* típus-e |
std::is_reference<T> |
referencia-e |
std::is_const<T> |
const-e |
std::is_class<T> |
osztály-e |
std::is_enum<T> |
felsorolt típus-e |
std::is_fundamental<T> |
alapvető típus-e (int, float, stb.) |
std::is_arithmetic<T> |
int, float, stb. |
std::remove_pointer<T> |
pointer eltávolítása |
std::add_const<T> |
const T típus létrehozása |
✅ Összegzés
| Fogalom | Leírás |
|---|---|
| Type traits könyvtár | Típusvizsgálat és típusmódosítás template-en belül |
| Előnye | Fordítási időben működik, hatékony |
| Használat | std::is_X, std::remove_X, std::enable_if, std::conditional |
| Modern alternatíva | C++20-tól: concepts, requires, std::is_same_v stb. |
- type traits library - Szótár.net (en-hu)
- type traits library - Sztaki (en-hu)
- type traits library - Merriam–Webster
- type traits library - Cambridge
- type traits library - WordNet
- type traits library - Яндекс (en-ru)
- type traits library - Google (en-hu)
- type traits library - Wikidata
- type traits library - Wikipédia (angol)