C++0x -- mapping an argument pack to a set of indices.

E

er

Hello,

Here's what I'd like to achieve:

template<typename ...Args>
void g(Args&&...args){}

template<typename ...Args>
void f( std::tuple<Args...> const& t )
{
//g(
// std::get<0>( t ),
// ...,
// std::get<sizeof...(Args)>( t )
//);
}

Any suggestion, please?
 
L

Larry Evans

Hello,

Here's what I'd like to achieve:

template<typename ...Args>
void g(Args&&...args){}

template<typename ...Args>
void f( std::tuple<Args...> const& t )
{
//g(
// std::get<0>( t ),
// ...,
// std::get<sizeof...(Args)>( t )
//);
}

Any suggestion, please?

what about:

g(std::get<Indices>(t)...)

where Indices is a parameter pack of indices.
Creating such an indices pack can be created with the
package_range_c template as shown here:


https://svn.boost.org/trac/boost/br...ndbox/tuple_non_recur.package_range_c.cpp#L54


Warning, NOT TESTED.

HTH.

-Larry
 
E

er

what about:

  g(std::get<Indices>(t)...)

where Indices is a parameter pack of indices.
Creating such an indices pack can be created with the
package_range_c template as shown here:

https://svn.boost.org/trac/boost/browser/sandbox/variadic_templates/l...

Warning, NOT TESTED.

HTH.

-Larry

Thanks. It seems to do more than I need but it definitely helped
thinking about the problem.

First, create a level of indirection between g() and f(),
f_impl(indices<I,Values...>, tuple). Second, Values... is mapped from
Args... using a straightforward recursion.

Below is a solution that compiled with GCC4.4 with flag -std=c++0x

#include <cstddef>
#include <tuple>
#include <iostream>

template<typename ...Args> struct pack_holder{};

template<typename I, I...Values> struct indices{};

namespace impl{

template<typename Indices,typename Pack> struct args_to_indices{};

template<typename I, I...Tail, typename T, typename... Rest>
struct args_to_indices<
{
typedef typename impl::args_to_indices<
indices said:
>::type type;
};

template<typename I, I...Tail>
struct args_to_indices<
indices said:
typedef indices<I, Tail...> type;
};

}// impl

template<typename I,typename...Args>
struct args_to_indices : impl::args_to_indices<
indices said:

void g(){}

template<typename T,typename...Args>
void g(T&& t, Args&&...args)
{
std::cout << t << ' ';
g( args... );
}


template<typename I,I ...Values, typename T>
void f_impl(indices<I, Values...>, T const& t )
{
g( std::get<Values>( t )...);
}


template<typename ...Args>
void f( std::tuple<Args...> const& t )
{
typedef int int_;
typedef typename args_to_indices<int_,Args...>::type indices_;
f_impl(indices_(), t);
}

int main(){

typedef std::tuple<int, const char*, double> tuple_;
tuple_ t(12, "Hello", 43.56);
f( t ); // outputs 12 "Hello" 43.56

return 0;
}
 
E

er

PS:
First, create a level of indirection between g() and f(),

Improper phrasing, in hindsight. A layer between.
template<typename T,typename...Args>
void g(T&& t, Args&&...args)
{
    std::cout << t << ' ';
    g( args... );

}


Maybe g( std::forward<Args>( args )... ) is safer...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top