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

लेसन 151 · Modern C++ and Beyond

std::bit_cast, mdspan और नई यूटिलिटीज़

C++20 और C++23 लाइब्रेरी की उपयोगी नई चीज़ें जानिए: std::bit_cast, bit यूटिलिटीज़, byteswap, to_underlying, flat_map, move_only_function और mdspan।

हर C++ स्टैंडर्ड कुछ छोटे टूल जोड़ता है, जो चुपचाप पुराने और ग़लती-प्रवण तरीक़ों की जगह ले लेते हैं। इनमें से कुछ तरीक़े आपने शायद ख़ुद भी लिखे होंगे: किसी float के बिट देखने के लिए memcpy, हाथ से लिखा byte swap, enum से उसकी इंटीजर वैल्यू में cast, या मैट्रिक्स का आभास देने के लिए एक सपाट (flat) array में चौड़ाई से गुणा करने वाला फ़ंक्शन। नए स्टैंडर्ड इनमें से हर एक को एक नाम और एक सटीक, सुरक्षित अर्थ देते हैं।

यह पाठ उन टूल्स से होकर गुज़रता है जो आपको सबसे ज़्यादा मिलने की संभावना है: std::bit_cast और <bit> हेडर, std::byteswap, std::to_underlying, std::flat_map, std::move_only_function, और std::mdspan, जो एक बहुआयामी view है और C++23 का हिस्सा है। ज़्यादातर उदाहरण यहाँ GCC 15 पर C++23 मोड में चलते हैं। कुछ सुविधाएँ इस रनर की स्टैंडर्ड लाइब्रेरी में उपलब्ध नहीं हैं, इसलिए वे साफ़ लेबल के साथ केवल पढ़ने वाले कोड के रूप में दिखाई गई हैं। हर स्टैंडर्ड में क्या बदला, इसकी बड़ी तस्वीर के लिए Modern C++ Features देखें।

std::bit_cast: undefined behavior के बिना bits की दोबारा व्याख्या

कभी-कभी आपको सचमुच किसी ऑब्जेक्ट के bits को किसी दूसरे type की तरह देखना होता है: किसी float के सटीक bits दिखाना, floating-point संख्या का hash निकालना, या कोई binary format पढ़ना। लुभावना तरीक़ा pointer cast है:

C++
float f = 1.5f;
std::uint32_t bits = *reinterpret_cast<std::uint32_t*>(&f);   // undefined behavior

इससे aliasing के नियम टूटते हैं, इसलिए कंपाइलर यह मानकर चल सकता है कि ऐसा कभी होता ही नहीं, और वह चौंकाने वाला कोड बना सकता है। लंबे समय से चला आ रहा सुरक्षित तरीक़ा std::memcpy से bytes कॉपी करना है। C++20 से std::bit_cast<To>(from) (हेडर <bit>) यह काम सीधे करता है: यह To type का एक नया ऑब्जेक्ट बनाता है, जिसके bits स्रोत के bits की कॉपी होते हैं। दोनों types का आकार समान होना चाहिए और वे trivially copyable होने चाहिए, और कंपाइलर इसकी जाँच करता है। यह constexpr है, इसलिए compile time पर भी काम करता है।

C++ किसी float के bits देखना

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.

floating-point संख्याएँ कैसे स्टोर होती हैं, इसके बारे में आप Floating-Point Numbers में और पढ़ सकते हैं। यहाँ मुख्य बात यह है: आपस में असंबंधित trivially copyable types के बीच type punning का मान्य तरीक़ा bit_cast है। यह non-trivial ऑब्जेक्ट्स के pointers के लिए नहीं है, और unions पर लागू नहीं होता, जिनके बारे में आप structs and unions में पढ़ते हैं।

टूलबॉक्स

इसी हेडर में unsigned इंटीजर (पूर्णांक) के लिए छोटे फ़ंक्शनों का एक सेट है, जिनके लिए पहले कंपाइलर-विशिष्ट built-ins चाहिए होते थे:

फ़ंक्शन अर्थ
std::popcount(x) 1 वाले bits की संख्या
std::has_single_bit(x) अगर x दो की घात (power of two) है तो true
std::bit_ceil(x) / std::bit_floor(x) x से बड़ी या बराबर सबसे छोटी दो की घात / x से छोटी या बराबर सबसे बड़ी
std::bit_width(x) x को दर्शाने के लिए ज़रूरी bits की संख्या
std::countl_zero(x) / std::countr_zero(x) शुरू के / आख़िर के शून्य bits
std::rotl(x, n) / std::rotr(x, n) bits को बाएँ या दाएँ घुमाना (rotate)
std::byteswap(x) (C++23) किसी इंटीजर का byte क्रम उलटना
std::endian little, big या native
C++ कंपाइलर built-ins के बिना bit की तरकीबें

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.

जब आप तय byte क्रम वाले binary formats पढ़ें या लिखें, जैसे network protocols (देखें Network Programming Basics) और binary files, तब byteswap इस्तेमाल करें। ये फ़ंक्शन केवल unsigned इंटीजर types स्वीकार करते हैं, जिससे नतीजे अच्छी तरह परिभाषित रहते हैं; पृष्ठभूमि के लिए Bitwise Operators देखें।

std::to_underlying: enum से इंटीजर, साफ़ इरादे के साथ

किसी enum को उसके underlying इंटीजर type में बदलने के लिए पहले static_cast<std::underlying_type_t<E>>(e) लिखना पड़ता था, जो लंबा है और type को दोहराता है। C++23 <utility> में std::to_underlying(e) जोड़ता है:

C++ scoped enums के लिए to_underlying

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.

यह किसी भी enum पर काम करता है, चाहे scoped हो या नहीं, और पढ़ने में वही लगता है जो यह करता है। scoped enum की बुनियादी बातों के लिए Enums देखें।

std::flat_map और flat_set (C++23)

std::map हर entry को अपने अलग heap node में रखता है। std::flat_map (हेडर <flat_map>) keys और values को दो क्रमबद्ध (sorted) लगातार (contiguous) containers में रखता है, जो डिफ़ॉल्ट रूप से दो std::vector होते हैं। lookups लगातार मेमोरी पर binary search करते हैं, जो cache के लिए बहुत अनुकूल है, और iteration तेज़ होता है। बदले में, बीच में insert या erase करने पर तत्व खिसकाने पड़ते हैं, इसलिए यह उस डेटा के लिए सबसे अच्छा है जो एक बार बनता है और कई बार पढ़ा जाता है, या छोटे maps के लिए। इसका interface std::map जैसा ही है:

C++ flat_map: sorted vectors में रखा map

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.

std::map की तुलना में, flat_map हर insert और erase पर iterators को अमान्य (invalidate) कर देता है, ठीक vector की तरह। इसे तब चुनें जब आपको सस्ते insertions से ज़्यादा पढ़ने की रफ़्तार और कम मेमोरी की ज़रूरत हो।

std::move_only_function (C++23)

std::function की माँग है कि उसमें रखा गया callable कॉपी किया जा सके। इससे वे lambdas बाहर हो जाते हैं जो std::unique_ptr, std::promise या अन्य move-only ऑब्जेक्ट capture करते हैं। std::move_only_function (हेडर <functional>) यह शर्त हटा देता है। task queues के लिए, जहाँ हर task एक बार चलता है, यह बेहतर type है:

C++ unique_ptr रखने वाले lambda को स्टोर करना

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.

वही lambda किसी std::function के अंदर कंपाइल नहीं होता। यह भी ध्यान रखें कि move_only_function अपने signature में const, noexcept और reference qualifiers का समर्थन करता है, और ख़ाली वाले को कॉल करना undefined behavior है, इसलिए पहले जाँच लें।

std::mdspan: बहुआयामी view (C++23)

आप std::span से मिल चुके हैं, जो एक-आयामी range पर non-owning view है। std::mdspan (हेडर <mdspan>) उसका बहुआयामी रिश्तेदार है: यह सपाट डेटा का pointer और extents लेता है और आपको कुछ भी कॉपी किए या अपना बनाए बिना m[row, col] syntax इस्तेमाल करने देता है (C++23 operator[] में कई indices की अनुमति देता है)। एक layout policy तय करती है कि indices मेमोरी पर कैसे map होंगे: layout_right C arrays की तरह row-major है, layout_left Fortran की तरह column-major है, और layout_stride sub-views के लिए मनचाहे strides की अनुमति देता है।

रनर की स्टैंडर्ड लाइब्रेरी अभी <mdspan> उपलब्ध नहीं कराती, इसलिए नीचे का उदाहरण केवल पढ़ने के लिए है। यह ऐसी लाइब्रेरी के साथ कंपाइल होगा जो C++23 mdspan लागू करती है, जैसे libstdc++ या libc++ का कोई हालिया संस्करण, या Kokkos प्रोजेक्ट का reference implementation:

C++
#include <mdspan>
#include <print>
#include <vector>

int main() {
    std::vector<int> storage(2 * 3);           // 2 rows, 3 columns, सपाट रूप में स्टोर
    std::mdspan grid(storage.data(), 2, 3);    // extents अपने-आप तय: dynamic 2 x dynamic 3

    for (std::size_t r = 0; r < grid.extent(0); ++r) {
        for (std::size_t c = 0; c < grid.extent(1); ++c) {
            grid[r, c] = static_cast<int>(r * 10 + c);
        }
    }
    std::println("{}", storage);              // [0, 1, 2, 10, 11, 12]
}

mdspan आपको indexing का गणित देता है, स्टोरेज नहीं। एक ही vector को एक भी byte कॉपी किए बिना 2 x 3, 3 x 2 या 6 x 1 के रूप में देखा जा सकता है। जब तक यह आपके toolchain में उपलब्ध न हो, आप इसी विचार वाला एक छोटा विकल्प (stand-in) लिख सकते हैं, यानी एक सपाट vector और उसकी चौड़ाई। इससे यह भी दिखता है कि mdspan क्या-क्या अपने-आप करता है:

C++ सपाट डेटा पर हाथ से लिखा 2D view

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.

आपको इनमें से कौन सा इस्तेमाल करना चाहिए?

flowchart TD
    Q["आपको क्या चाहिए?"] --> A{"किसी वैल्यू के bits को<br/>दूसरे type की तरह पढ़ना?"}
    A -->|हाँ| B["std::bit_cast"]
    A -->|नहीं| C{"फ़ाइल या protocol के लिए<br/>तय byte क्रम?"}
    C -->|हाँ| D["std::byteswap + std::endian"]
    C -->|नहीं| E{"सपाट डेटा पर मैट्रिक्स<br/>या grid view?"}
    E -->|हाँ| F["std::mdspan"]
    E -->|नहीं| G["सारांश तालिका देखें"]

इस पाठ के तीन टूल्स में से चुनने का एक तेज़ रास्ता।

लाइब्रेरी का समर्थन कंपाइलरों और संस्करणों के बीच अलग-अलग होता है, इसलिए किसी C++23 सुविधा पर निर्भर होने से पहले अपने toolchain का documentation देखें, और उसी कंपाइलर से टेस्ट करें जिससे आप सॉफ़्टवेयर भेजते हैं। feature-test macros (जैसे __cpp_lib_bit_cast, जो <version> में परिभाषित है) compile time पर बताते हैं कि कोई सुविधा मौजूद है या नहीं।

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

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. किसी float f के bits पाने के लिए *reinterpret_cast<std::uint32_t*>(&f) ख़राब तरीक़ा क्यों है?

    किसी असंबंधित इंटीजर type के pointer से float को access करना strict aliasing तोड़ता है। std::bit_cast (या memcpy) यही काम करने का परिभाषित तरीक़ा है।
  2. 2. std::bit_cast<To>(from) के कंपाइल होने के लिए कौन सी शर्त पूरी होनी चाहिए?

    यह फ़ंक्शन ऑब्जेक्ट का representation कॉपी करता है, इसलिए आकार मेल खाने चाहिए और दोनों types trivially copyable होने चाहिए।
  3. 3. std::function की तुलना में std::move_only_function का मुख्य फ़ायदा क्या है?

    std::function को कॉपी होने लायक callables चाहिए। move_only_function को केवल उनका movable होना चाहिए, इसलिए यह ऐसे एक बार चलने वाले tasks के लिए ठीक है जिनके पास resources हैं।
  4. 4. std::mdspan जिस डेटा को index करता है, उसका मालिक होता है।

    std::span की तरह mdspan एक view है। यह एक pointer और extents रखता है, और स्टोरेज का जीवनकाल इससे लंबा होना चाहिए।
  5. 5. std::map की तुलना में std::flat_map कब अच्छा विकल्प है?

    flat_map sorted keys और values को लगातार arrays में रखता है, जिससे lookups और iteration तेज़ होते हैं, पर insertions तत्वों को खिसकाते हैं और iterators को अमान्य कर देते हैं।

सारांश

  • std::bit_cast किसी ऑब्जेक्ट के bits को समान आकार वाले दूसरे trivially copyable type में सुरक्षित रूप से और compile time पर भी कॉपी करता है।
  • <bit> में popcount, bit_width, bit_ceil, rotl, countr_zero, std::endian और (C++23) byteswap मिलते हैं।
  • std::to_underlying type लिखे बिना किसी enum को उसकी इंटीजर वैल्यू में बदलता है।
  • std::flat_map sorted map को लगातार vectors में रखता है: ज़्यादा पढ़े जाने वाले डेटा के लिए बढ़िया।
  • std::move_only_function move-only callables रखता है, जैसे ऐसे lambdas जिनके पास unique_ptr है।
  • std::mdspan सपाट मेमोरी पर बिना कॉपी किए बहुआयामी indexing देता है; जाँच लें कि आपकी लाइब्रेरी में यह है या नहीं।

अगला पाठ: Project: Grade Book, जहाँ आप इस भाषा को एक पूरे प्रोग्राम में एक साथ जोड़ते हैं।

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

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