सामग्री पर जाएँ

लेसन 92 · Templates and Generic Programming

Type Traits और मेटाप्रोग्रामिंग

C++ type traits और मेटाप्रोग्रामिंग सीखें: is_integral, remove_reference, conditional, अपने trait लिखना, enable_if वाला SFINAE बनाम concepts और std::declval।

टेम्पलेट के अंदर आपको अक्सर किसी टाइप के बारे में सवाल पूछने होते हैं, या उससे नया टाइप निकालना होता है। क्या T एक इंटीजर है? क्या वह पॉइंटर है, और अगर हाँ, तो किसका? अगर T const std::string& है, तो उसके नीचे सादा std::string क्या है? दो टाइप में से कौन-सा बड़ा है? <type_traits> हेडर इन सबका जवाब कंपाइल टाइम पर देता है। हर जवाब एक type trait है: एक छोटा क्लास टेम्पलेट जो या तो bool वैल्यू या नतीजे वाला टाइप रखता है।

वैल्यू के बजाय टाइप से गणना करने वाले कोड को टेम्पलेट मेटाप्रोग्रामिंग (template metaprogramming) कहते हैं। इस पाठ में आप दो तरह के trait (query और transformation), _v और _t शॉर्टकट, टेम्पलेट स्पेशलाइज़ेशन से trait कैसे लागू होते हैं, अपने trait कैसे लिखें, और पुरानी std::enable_if वाली SFINAE तकनीक सीखेंगे जिसकी जगह concepts ने काफ़ी हद तक ले ली है। आप std::declval से भी मिलेंगे, जो उन टाइप पर एक्सप्रेशन के बारे में बात करने की तरकीब है जिन्हें आप बना नहीं सकते।

Query traits: हाँ/नहीं वाले सवाल पूछना

query trait में bool टाइप का एक स्टैटिक मेंबर value होता है। C++17 से हर एक का _v पर खत्म होने वाला वेरिएबल टेम्पलेट भी है, इसलिए std::is_integral<int>::value को std::is_integral_v<int> लिखा जा सकता है।

C++ टाइप के बारे में सवाल पूछना

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

trait कुछ परिवारों में आते हैं:

परिवार उदाहरण
प्राथमिक श्रेणियाँ is_void, is_integral, is_floating_point, is_pointer, is_lvalue_reference, is_enum, is_class, is_array, is_function
संयुक्त श्रेणियाँ is_arithmetic, is_fundamental, is_scalar, is_object, is_reference, is_member_pointer
गुण is_const, is_signed, is_unsigned, is_abstract, is_polymorphic, is_final, is_aggregate, is_trivially_copyable, is_standard_layout, has_virtual_destructor
ऑपरेशन is_constructible, is_default_constructible, is_copy_assignable, is_nothrow_move_constructible, is_invocable, is_swappable
संबंध is_same, is_base_of, is_convertible

"ऑपरेशन" वाले कई trait परफ़ॉर्मेंस के फ़ैसलों के लिए मायने रखते हैं। std::vector बढ़ते समय मूव करने और कॉपी करने में चुनने के लिए is_nothrow_move_constructible इस्तेमाल करता है (देखें Move Semantics), और कॉपी करने वाले एल्गोरिदम एक ही memmove पर जाने के लिए is_trivially_copyable इस्तेमाल करते हैं।

Transformation traits: नए टाइप निकालना

transformation trait में type नाम का नेस्टेड टाइप एलियास होता है। C++14 से हर एक का _t पर खत्म होने वाला एलियास है, इसलिए typename std::remove_reference<T>::type बन जाता है std::remove_reference_t<T>।

Trait बदलता है इसमें
remove_reference_t<T> int&, int&& int
remove_const_t<T> const int int
remove_cvref_t<T> (C++20) const int& int
remove_pointer_t<T> int* int
add_const_t<T>, add_pointer_t<T>, add_lvalue_reference_t<T> int const int, int*, int&
decay_t<T> const int&, int[4], void(int) int, int*, void(*)(int)
make_unsigned_t<T>, make_signed_t<T> int unsigned int
underlying_type_t<E> enum class E : char char
conditional_t<B, T, F> B true हो तो T, वरना F
common_type_t<Ts...> int, double double
invoke_result_t<F, Args...> एक callable और उसके आर्ग्युमेंट उसका रिटर्न टाइप

आप std::println से टाइप को सीधे प्रिंट नहीं कर सकते, पर std::is_same_v से किसी बदलाव की जाँच कर सकते हैं:

C++ टाइप को बदलना

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

inspect दिखाता है कि remove_cvref_t क्यों मायने रखता है: forwarding reference के साथ lvalue के लिए T std::string&, const lvalue के लिए const std::string& और अस्थायी ऑब्जेक्ट के लिए std::string होता है। T की सीधे std::string से तुलना पहले दो मामलों में फ़ेल होती। पहले रेफ़रेंस और const हटाएँ, फिर अपना सवाल पूछें। यह टेम्पलेट कोड के सबसे आम बग में से एक है।

std::conditional_t कंपाइल टाइम का ?: है: दोनों शाखाएँ वैध टाइप होनी चाहिए, और वह एक चुनता है। इसे नेस्ट करने से कई में से चुनाव होता है। std::to_underlying (C++23) enum को उसके अंतर्निहित इंटीजर में बदलता है, जो static_cast<std::underlying_type_t<E>>(e) का शॉर्टकट है।

trait कैसे लागू होते हैं

ज़्यादातर trait में कोई जादू नहीं है। वे स्पेशलाइज़ेशन वाले क्लास टेम्पलेट हैं, जो अक्सर दो सहायकों पर बने हैं: std::true_type और std::false_type, ऐसी क्लासें जिनका value true या false है (दोनों std::integral_constant<bool, B> के एलियास हैं)। यहाँ तीन स्टैंडर्ड trait के घर के बने संस्करण हैं:

C++ अपने trait लिखना

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

ध्यान दें कि is_vector<const std::vector<int>> false है: partial specialization ठीक std::vector<T, Alloc> से मेल खाती है, उसके const रूप से नहीं। स्टैंडर्ड trait भी ऐसे ही व्यवहार करते हैं (std::is_pointer_v<int* const> सिर्फ़ इसलिए true है क्योंकि is_pointer जानबूझकर पहले const हटाता है), इसलिए जब आपका मतलब "कोई भी वेक्टर" हो तो cv-qualifier खुद हटाएँ।

कुछ trait सादे C++ में लिखे ही नहीं जा सकते, जैसे is_enum, is_union, is_aggregate, is_trivially_copyable और underlying_type। कंपाइलर इन्हें __is_enum(T) जैसे बिल्ट-इन intrinsic से लागू करते हैं। स्टैंडर्ड वाले इस्तेमाल करने की यह एक और वजह है।

SFINAE और std::enable_if

C++20 से पहले concepts नहीं थे, फिर भी लाइब्रेरी को "यह ओवरलोड सिर्फ़ इंटीजर के लिए" जैसी चीज़ चाहिए थी। वे SFINAE नाम के नियम का इस्तेमाल करती थीं: Substitution Failure Is Not An Error (सब्स्टिट्यूशन का फ़ेल होना एरर नहीं)। जब कंपाइलर डिड्यूस किए टाइप को टेम्पलेट के सिग्नेचर में रखता है और नतीजा अवैध होता है, तो वह एरर नहीं देता। वह बस उस टेम्पलेट को उम्मीदवारों के समूह से हटा देता है।

std::enable_if_t<Condition, T> इसका फ़ायदा उठाता है। अगर Condition true है, तो वह टाइप T है (डिफ़ॉल्ट void); false होने पर उसमें type मेंबर नहीं होता, इसलिए सब्स्टिट्यूशन फ़ेल होता है और ओवरलोड गायब हो जाता है।

C++ enable_if वाला SFINAE बनाम concepts

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

दोनों वर्ज़न एक ही काम करते हैं, पर पठनीयता की तुलना करें। SFINAE की जानी-पहचानी समस्याएँ हैं:

  • इरादा असामान्य सिंटैक्स में छिपा रहता है: डिफ़ॉल्ट वाला बिना नाम का नॉन-टाइप टेम्पलेट पैरामीटर, या decltype रिटर्न टाइप।
  • एरर संदेश असली ज़रूरत के बजाय "no type named 'type' in enable_if" की बात करते हैं।
  • ओवरलोड को रैंक नहीं किया जा सकता: बचे हुए दो SFINAE ओवरलोड बस अस्पष्ट (ambiguous) होते हैं, जबकि concepts सबसे ज़्यादा constrained वाले को चुनते हैं।
  • बॉडी के अंदर की विफलता फिर भी पक्का एरर है; सिर्फ़ "immediate context" (सिग्नेचर) की विफलताएँ SFINAE के अनुकूल हैं।

नए कोड में concepts इस्तेमाल करें। फिर भी आपको SFINAE पढ़ना आना चाहिए, क्योंकि मौजूदा लाइब्रेरी कोड की बड़ी मात्रा (और स्टैंडर्ड लाइब्रेरी खुद, अंदर से) इसका इस्तेमाल करती है। std::void_t वाला "detection idiom", जो आपको दिख सकता है, C++20 से पहले "क्या यह एक्सप्रेशन कंपाइल होता है?" पूछने का एक और तरीका है, जिसकी जगह अब requires एक्सप्रेशन ने ले ली है।

std::declval: बिना ऑब्जेक्ट के टाइप पर एक्सप्रेशन

कभी-कभी आपको T वाले किसी एक्सप्रेशन का टाइप चाहिए, पर आप T बना नहीं सकते (शायद उसका कोई डिफ़ॉल्ट कंस्ट्रक्टर न हो)। std::declval<T>() (<utility> में) T&& टाइप की वैल्यू बनाने का दिखावा करता है। यह सिर्फ़ unevaluated context में आ सकता है, जैसे decltype, sizeof और noexcept(...); इसे सच में कॉल करना कंपाइल एरर है।

C++ unevaluated context में std::declval

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Engine को बिल्कुल बनाया नहीं जा सकता, फिर भी आप पूछ सकते हैं कि horsepower() क्या लौटाता है और वह noexcept है या नहीं। C++20 कोड में पैरामीटर वाला requires एक्सप्रेशन (requires(const T& t) { t.size(); }) declval के कई पुराने काम ज़्यादा पठनीय ढंग से कर देता है, पर टाइप निकालने के लिए टूल अब भी declval ही है।

आधुनिक C++ में मेटाप्रोग्रामिंग: क्या इस्तेमाल करें

  • टाइप के बारे में सवाल पूछें: if constexpr और requires में std::is_*_v trait, या उन पर बने स्टैंडर्ड concepts।
  • टाइप निकालें: _t transformation trait (remove_cvref_t, conditional_t, common_type_t, invoke_result_t) और alias template।
  • वैल्यू निकालें: रिकर्सिव टेम्पलेट नहीं, बल्कि constexpr और consteval फ़ंक्शन (देखें कंपाइल-टाइम प्रोग्रामिंग); उन्हें पढ़ना कहीं आसान है और वे तेज़ कंपाइल होते हैं।
  • ओवरलोड सीमित करें: enable_if नहीं, concepts।
  • अपना trait तभी लिखें जब स्टैंडर्ड में कुछ उपयुक्त न हो, और उसे _v या _t एलियास दें।

C++26 स्टैटिक रिफ़्लेक्शन (^^T और std::meta) जोड़ता है, जिससे कोड मेंबर, नाम और enum को सीधे जाँच सकेगा। यह GCC 15 के C++23 मोड में उपलब्ध नहीं है, इसलिए यहाँ इसे सिर्फ़ आने वाली चीज़ के रूप में बताया गया है।

अपनी समझ जाँचें

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. template <typename T> void f(T&& x) के अंदर, आप std::string name के साथ f(name) कॉल करते हैं। std::is_same_v<T, std::string> क्या है?

    lvalue आर्ग्युमेंट के लिए forwarding reference T को lvalue reference डिड्यूस करता है। तुलना से पहले std::remove_cvref_t<T> इस्तेमाल करें।
  2. 2. 4 बाइट int वाले सामान्य प्लेटफ़ॉर्म पर std::conditional_t<(sizeof(int) > 8), long, short> क्या है?

    शर्त false है, इसलिए conditional_t तीसरा आर्ग्युमेंट short चुनता है।
  3. 3. जब डिड्यूस किए टाइप को टेम्पलेट के सिग्नेचर में रखने से अवैध टाइप बनता है, तो SFINAE क्या करता है?

    Substitution Failure Is Not An Error। टेम्पलेट बस उम्मीदवार नहीं रहता; अगर कोई उम्मीदवार न बचे, तब आपको "no matching function" एरर मिलता है।
  4. 4. std::declval<T>() कहाँ इस्तेमाल हो सकता है?

    declval की ऐसी कोई डेफ़िनिशन नहीं जिसे आप चला सकें; वह सिर्फ़ T&& बनाने का दिखावा करता है ताकि कंपाइलर टाइप निकाल सके। evaluated कोड में इसका इस्तेमाल कंपाइल एरर है।
  5. 5. template <typename T> struct is_vector : std::false_type {}; और std::vector<T, A> की partial specialization दिए होने पर, is_vector<const std::vector<int>>::value क्या है?

    const std::vector<int> पैटर्न std::vector<T, A> से मेल नहीं खाता, इसलिए primary template (false_type) इस्तेमाल होता है। अगर आपका मतलब कोई भी वेक्टर है तो पहले cv-qualifier हटाएँ।

सारांश

  • <type_traits> कंपाइल टाइम पर query traits (is_integral_v<T>, is_same_v<T, U>, is_nothrow_move_constructible_v<T>) और transformation traits (remove_cvref_t<T>, decay_t<T>, conditional_t<B, T, F>) देता है।
  • _v और _t शॉर्टकट इस्तेमाल करें; डिड्यूस किए टाइप की तुलना से पहले remove_cvref_t से रेफ़रेंस और const हटाएँ।
  • trait स्पेशलाइज़ेशन वाले क्लास टेम्पलेट हैं, जो आमतौर पर std::true_type/std::false_type से डिराइव होते हैं; कुछ कंपाइलर intrinsic पर निर्भर हैं।
  • SFINAE उन टेम्पलेट को हटाता है जिनका सिग्नेचर सब्स्टिट्यूशन में फ़ेल होता है; std::enable_if ओवरलोड सीमित करने के लिए इसका इस्तेमाल करता है। concepts वही काम पठनीय ढंग से करते हैं और ओवरलोड को रैंक कर सकते हैं।
  • std::declval<T>() आपको टाइप बनाए बिना decltype, sizeof और noexcept में उस पर एक्सप्रेशन बनाने देता है।
  • कंपाइल-टाइम वैल्यू के लिए constexpr फ़ंक्शन और constraint के लिए concepts को प्राथमिकता दें; टाइप के बारे में पूछने और टाइप निकालने के लिए trait इस्तेमाल करें।

अगला पाठ: स्टैंडर्ड टेम्पलेट लाइब्रेरी, जहाँ कंटेनर, इटरेटर और एल्गोरिदम एक साथ आते हैं।

अपनी प्रगति ट्रैक करें

मुफ़्त अकाउंट बनाएँ, लेसन पूरे के रूप में चिह्नित करें और किसी भी डिवाइस पर वहीं से शुरू करें जहाँ छोड़ा था।