Re: C++0x Variadic Tuple Manipulation
On 6 Mrz., 14:57, PGK wrote:
If I have a variadic template class such as:
template <typename ... Types>
struct FooD {
std::tuple<std::vector<Types> ...> vars;
};
how can I define a member function which will
have the same number of arguments as the class has
template parameters, but different types.
For example, how would I write a function with n
int-type arguments. The n-th argument, xn, would delete
the xn-th element of the n-th (vector) member of the
tuple, "vars" (in the above class, FooD).
One possible approach:
#include <vector>
#include <tuple>
#include <type_traits>
template <typename ... Types>
struct FooD {
std::tuple<std::vector<Types> ...> vars;
void multi_delete(
typename std::conditional<true,long,Types>::type... indices);
};
namespace FooD_details {
template <typename VectorTupleType>
inline void multi_delete_helper(VectorTupleType & vt) {}
template <typename VectorTupleType, typename ... MoreIndices>
inline void multi_delete_helper(
VectorTupleType & vt, int delIndex, MoreIndices ... mi)
{
constexpr int members = std::tuple_size<VectorTupleType>::value;
constexpr int member = members - (1 + sizeof...(mi));
auto & vecref = get<member>(vt);
vecref.erase(vecref.begin() + delIndex);
multi_delete_helper(vt,mi...);
}
} // namespace FooD_details
template <typename ... Types>
void FooD<Types...>::multi_delete(
typename std::conditional<true,int,Types>::type... indices)
{
FooD_details::multi_delete_helper(vars,indices...);
}
(I didn't test this code)
It's rather verbose, but you can probably make it more generic so
it'll be easier to "iterate" over the tuple's elements and have
arbirary actions performed on each element.
Cheers,
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]