खोज…


परिचय

वर्ग टेम्पलेट std::integer_sequence<Type, Values...> के प्रकार के मूल्यों का एक अनुक्रम का प्रतिनिधित्व करता है Type जहां Type निर्मित पूर्णांक प्रकार से एक है। इन दृश्यों का उपयोग क्लास या फ़ंक्शन टेम्प्लेट को लागू करते समय किया जाता है जो स्थितिगत पहुंच से लाभान्वित होते हैं। मानक पुस्तकालय में "फ़ैक्टरी" प्रकार भी होते हैं जो तत्वों की संख्या से पूर्णांक मूल्यों के आरोही क्रम बनाते हैं।

एक std :: tuple को चालू करें फ़ंक्शन मापदंडों में

एक std::tuple<T...> का उपयोग कई मानों को पास करने के लिए किया जा सकता है। उदाहरण के लिए, यह एक कतार के कुछ रूप में मापदंडों के अनुक्रम को संग्रहीत करने के लिए इस्तेमाल किया जा सकता है। इस तरह के एक कार्य को संसाधित करते समय इसके तत्वों को फ़ंक्शन कॉल तर्क में बदलना पड़ता है:

#include <array>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>

// ----------------------------------------------------------------------------
// Example functions to be called:
void f(int i, std::string const& s) {
    std::cout << "f(" << i << ", " << s << ")\n";
}
void f(int i, double d, std::string const& s) {
    std::cout << "f(" << i << ", " << d << ", " << s << ")\n";
}
void f(char c, int i, double d, std::string const& s) {
    std::cout << "f(" << c << ", " << i << ", " << d << ", " << s << ")\n";
}
void f(int i, int j, int k) {
    std::cout << "f(" << i << ", " << j << ", " << k << ")\n";
}

// ----------------------------------------------------------------------------
// The actual function expanding the tuple:
template <typename Tuple, std::size_t... I>
void process(Tuple const& tuple, std::index_sequence<I...>) {
    f(std::get<I>(tuple)...);
}

// The interface to call. Sadly, it needs to dispatch to another function
// to deduce the sequence of indices created from std::make_index_sequence<N>
template <typename Tuple>
void process(Tuple const& tuple) {
    process(tuple, std::make_index_sequence<std::tuple_size<Tuple>::value>());
}

// ----------------------------------------------------------------------------
int main() {
    process(std::make_tuple(1, 3.14, std::string("foo")));
    process(std::make_tuple('a', 2, 2.71, std::string("bar")));
    process(std::make_pair(3, std::string("pair")));
    process(std::array<int, 3>{ 1, 2, 3 });
}

जब तक एक वर्ग std::get<I>(object) का समर्थन करता है std::get<I>(object) और std::tuple_size<T>::value उपरोक्त process() फ़ंक्शन के साथ विस्तारित किया जा सकता है। फ़ंक्शन स्वयं तर्कों की संख्या से पूरी तरह से स्वतंत्र है।

पूर्णांकों से मिलकर एक पैरामीटर पैक बनाएं

std::integer_sequence अपने आप में पूर्णांकों के अनुक्रम को रखने के बारे में है जिसे एक पैरामीटर पैक में बदल दिया जा सकता है। इसका प्राथमिक मूल्य इन अनुक्रमों को बनाने वाले "कारखाना" वर्ग टेम्पलेट बनाने की संभावना है:

#include <iostream>
#include <initializer_list>
#include <utility>

template <typename T, T... I>
void print_sequence(std::integer_sequence<T, I...>) {
    std::initializer_list<bool>{ bool(std::cout << I << ' ')... };
    std::cout << '\n';
}

template <int Offset, typename T, T... I>
void print_offset_sequence(std::integer_sequence<T, I...>) {
    print_sequence(std::integer_sequence<T, T(I + Offset)...>());
}

int main() {
    // explicitly specify sequences:
    print_sequence(std::integer_sequence<int, 1, 2, 3>());
    print_sequence(std::integer_sequence<char, 'f', 'o', 'o'>());

    // generate sequences:
    print_sequence(std::make_index_sequence<10>());
    print_sequence(std::make_integer_sequence<short, 10>());
    print_offset_sequence<'A'>(std::make_integer_sequence<char, 26>());
}

मूल्यांकन के क्रम की गारंटी देने के लिए पूर्णांक अनुक्रम का विस्तार करते हुए और अप्रयुक्त [सरणी] चर का निर्माण नहीं करते समय print_sequence() फ़ंक्शन टेम्पलेट एक std::initializer_list<bool> का उपयोग करता है।

किसी तत्व की प्रतियों में सूचकांकों के अनुक्रम को चालू करें

कॉमा अभिव्यक्ति में सूचकांकों के पैरामीटर पैक को एक मूल्य के साथ विस्तारित करना प्रत्येक सूचकांकों के लिए मूल्य की एक प्रति बनाता है। अफसोस की बात है, gcc और clang लगता है कि सूचकांक का कोई प्रभाव नहीं है और इसके बारे में चेतावनी देते हैं ( gcc को इंडेक्स को void करने से चुप किया जा सकता है):

#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>
#include <utility>

template <typename T, std::size_t... I>
std::array<T, sizeof...(I)> make_array(T const& value, std::index_sequence<I...>) {
    return std::array<T, sizeof...(I)>{ (I, value)... };
}

template <int N, typename T>
std::array<T, N> make_array(T const& value) {
    return make_array(value, std::make_index_sequence<N>());
}

int main() {
    auto array = make_array<20>(std::string("value"));
    std::copy(array.begin(), array.end(),
              std::ostream_iterator<std::string>(std::cout, " "));
    std::cout << "\n";
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow