Reputation: 133
I have a templated class A in which I want to call a templated function from another class B
the header looks like :
template<typename... T> class A
{
public:
A();
void get(type_X params);
private:
B b_;
};
and the .hxx :
template<typename... T> void A<T...>::get(type_X params)
{
/*here lies my problem*/
T... variable; // just like we would do string s;
// and pass it to a function f(string& s) to assign it
b_.get(params, variable...);
/* do something with updated value of variable */
}
where member b_ (class B) has a templated function get which looks like
template<typename... T> int B<T...>::get(type_X params, const T&... variable)
{
/* change variable, just like we would assign a new value to a string& */
return 0;
}
And I have no idea how to initialize (if possible) my "T..." object to be given as argument to templated function B::get.
Thanks for your help
Upvotes: 3
Views: 2312
Reputation: 217283
Following may help you:
#if 1 // Not in C++11
// Helper class to be able to use expansion of std::get<Index>(tuple)
template <int... Is> struct index_sequence {};
// Following create index_sequence<0, 1, 2, .., sizeof...(Is) - 1>
template <int Index, int... Is>
struct make_index_sequence { // recursively build a sequence of indices
typedef typename make_index_sequence<Index - 1, Index -1, Is...>::type type;
};
template <int... Is>
struct make_index_sequence<0, Is...> { // stop the recursion when 0 is reached
typedef index_sequence<Is...> type;
};
#endif
template<typename... Ts> class A
{
public:
void get(type_X params)
{
std::tuple<Ts...> t;
a.get(params, t, make_index_sequence<sizeof...(Ts)>());
}
private:
template<std::size_t ... Is>
void get(type_X params, std::tuple<Ts...>& t, index_sequence<Is...>)
{
b_.get(params, std::get<Is>(t)...);
}
private:
B b_;
};
Upvotes: 0
Reputation: 71989
First off, this is invalid syntax:
T... variable;
To do something like this, you need to create a tuple:
std::tuple<T...> variable;
And then you use one of the techniques to call a function with the tuple members as parameters:
"unpacking" a tuple to call a matching function pointer Pass tuple's content as variadic function arguments How do I expand a tuple into variadic template function's arguments?
Upvotes: 1
Reputation: 55395
First off, the indices machinery to create a pack of integral indices at compile time:
template< std::size_t... Ns >
struct indices {
typedef indices< Ns..., sizeof...( Ns ) > next;
};
template< std::size_t N >
struct make_indices {
typedef typename make_indices< N - 1 >::type::next type;
};
template<>
struct make_indices< 0 > {
typedef indices<> type;
};
Now, you can create an array of objects:
T vars[] = { T()... };
Next, you need a helper function to create a context where you can deduce the pack of integers. Something like this:
template<typename... T, T, size_t... I>
int helper(type_X params, T* arr, indices<I...>)
{
return b_.get(params, arr[I]...);
}
For arr
parameter you'd pass the array vars
you created previously and for the last one you pass make_indices<sizeof...(T)>::type()
.
Hope that helps.
Upvotes: 3