Quick Links: |
Provide a template parameter pack of integers. More...
bslmf::MakeIntegerSequence | compile-time integer sequence factory function |
bslmf::MakeIntegerSequence
that generate a compile-time sequence of increasing integer values starting with 0. bslmf::MakeIntegerSequence
meets the requirements of the make_integer_sequence
template introduced in the C++14 standard [intseq.intseq] and can be used in a client's code if the compiler supports the C++11 standard. t_N
with data read from a data source using a library class that provides a variadic template interface that loads a data of variable length into the supplied parameter pack. DataReader
, template <std::size_t t_N> class DataReader { public:
args
with data read from a data source. template <class ...t_T> void read(t_T*... args) const { static_assert(sizeof...(args) == t_N, ""); read_impl(args...); }
read_impl
function that streams the index of the C-Array's element to stdout
. private: template <class U, class ...t_T> void read_impl(U*, t_T*... args) const { printf("read element #%i\n", static_cast<int>(t_N - 1 - sizeof...(args))); read_impl(args...); }
void read_impl() const { } };
readData
that expands the parameter pack of indices I
and invokes the variadic template read
method of the specified reader
object. namespace { template<class R, class t_T, std::size_t... I> void readData(const R& reader, t_T *data, bslmf::IntegerSequence<std::size_t, I...>) { reader.read(&data[I]...); // In pseudocode, this is equivalent to: // reader.read(&data[0], // &data[1], // &data[2], // ... // &data[t_N-1]); } }
readData
that invokes the helper function Note, that the bslmf::MakeIntegerSequence<std::size_t, t_N>
function generates an object of an integer sequence class instantiated with a template parameter pack of integers that will be expanded and used as an array's indices in the helper function when calling the Reader::read(t_T*...)
variadic template function. template<class t_T, std::size_t t_N> void readData(const DataReader<t_N>& reader, t_T *data) { readData(reader, data, bslmf::MakeIntegerSequence<std::size_t, t_N>()); }
data
C-Array and reader
variables and pass them to the readData
function as parameters. constexpr int k_SIZE = 5; DataReader<k_SIZE> reader; int data[k_SIZE] = {0}; readData(reader, data);
stdout
: read element #0 read element #1 read element #2 read element #3 read element #4