You can use the expander trick. This needs C++14 though. I don't know what your byte array format looks like, so I just took a generic struct with a string member and convert everything to string in the constructor.
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
template < typename T, typename F, size_t ... Is >
void for_each_impl(T&& t, F&& f, std::index_sequence<Is...>)
{
using expand_type = int[];
(void) expand_type { 0, ( (void) f(std::get<Is>(t)), 0) ... };
}
template < typename... Args, typename F >
void for_each(std::tuple<Args...> const& t, F&& f)
{
for_each_impl(t, f, std::make_index_sequence<sizeof...(Args)>{});
}
struct Bytes {
std::string str;
Bytes(char const * c) : str(c) {};
Bytes(int i) : str(std::to_string(i)) {};
Bytes(char c) : str(1, c) {};
};
int main()
{
auto t = std::make_tuple(12, "abc", 10, 'c', 1);
std::vector<Bytes> v;
for_each(t, [&v](auto&& x){ v.push_back(x); });
for (auto const& e : v)
std::cout << e.str << ' ';
std::cout << '\n';
}
Live example
In C++17 it is much easier, thanks to variadic lambda, fold expression, and std::apply
.
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
struct Bytes {
std::string str;
Bytes(char const * c) : str(c) {};
Bytes(int i) : str(std::to_string(i)) {};
Bytes(char c) : str(1, c) {};
};
int main()
{
auto t = std::make_tuple(12, "abc", 10, 'c', 1);
std::vector<Bytes> v;
std::apply([&v](auto&&... x){ (... , v.push_back(x)); }, t);
for (auto const& e : v)
std::cout << e.str << ' ';
std::cout << '\n';
}
Live example
If you cannot use C++11 your life will be painful. You have to roll your own implementation of index_sequence
and you have to define a helper struct with templated call operator to substitute the generic lambda.
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
// https://stackoverflow.com/a/24481400/1944004
template <size_t ...I>
struct index_sequence {};
template <size_t N, size_t ...I>
struct make_index_sequence : public make_index_sequence<N - 1, N - 1, I...> {};
template <size_t ...I>
struct make_index_sequence<0, I...> : public index_sequence<I...> {};
// Call a function for each element in a tuple
template < typename T, typename F, size_t ... Is >
void for_each_impl(T&& t, F&& f, index_sequence<Is...>)
{
using expand_type = int[];
(void) expand_type { 0, ( (void) f(std::get<Is>(t)), 0) ... };
}
template < typename... Args, typename F >
void for_each(std::tuple<Args...> const& t, F&& f)
{
for_each_impl(t, f, make_index_sequence<sizeof...(Args)>{});
}
// "Byte array" emulation
struct Bytes {
std::string str;
Bytes(char const * c) : str(c) {};
Bytes(int i) : str(std::to_string(i)) {};
Bytes(char c) : str(1, c) {};
};
// Surrogate template lambda
struct Visitor
{
std::vector<Bytes>& v;
Visitor(std::vector<Bytes>& vb) : v(vb) {};
template < typename T >
void operator() (T&& x) { v.push_back(x); }
};
int main()
{
auto t = std::make_tuple(12, "abc", 10, 'c', 1);
std::vector<Bytes> v;
for_each(t, Visitor(v));
for (auto const& e : v)
std::cout << e.str << ' ';
std::cout << '\n';
}
Live example