On 01.09.2018 17:31, Stefan Ram wrote:
> Can anyone here write an implementation of f so that
>
> f( g, 1, 2, 3, 4, 5, 6 )
>
> or
>
> f( g, 1, 2, 3, 4, 5, 6, 7 )
>
> will call
>
> g( 2, 4, 6 )
>
> , e.g., using every other argument with zero runtime
> overhead, e.g., using constexpr or templates.
>
> One can write f to get the parameters in a parameter
> pack and then possibly would need to write recursive
> partial specializations so as to unpack the parameter
> pack while building a new parameter pack with every
> other argument. (That last thing, building that new
> parameter pack and then using it for the call is what
> I have not yet learned to do!).
>
> If there would be an answer, I would then use it in
> an ongoing discussion in comp.lang.misc.
>
> I already found »struct alternating_tuple« at
>
>
www.cplusplus.com/forum/general/135558/
>
> which might go somewhat in the direction intended.
>
#include <stddef.h> // size_t
#include <tuple> // std::(tie, tuple)
#include <utility> // std::make_index_sequence
namespace my
{
//using std::forward_as_tuple;
using std::make_index_sequence;
using std::tie;
using std::tuple;
namespace impl
{
using std::get;
using std::index_sequence;
template< class Func, class Argsref_tuple, size_t... Indices >
void f(
const Func& func,
const Argsref_tuple& args,
index_sequence<Indices...>
)
{ func( get<1 + 2*Indices>( args )... ); }
}
template< class Func, class... Args >
void f( const Func& func, Args&&... args )
{
impl::f(
func,
//forward_as_tuple<Args...>( args... ),
std::tuple<Args&&...>( std::forward<Args>( args )... ),
make_index_sequence<sizeof...(args)/2>()
);
}
} // namespace my
#include <stdio.h>
using Int = const int;
void g( Int a, Int b, Int c )
{
printf( "%d %d %d\n", a, b, c );
}
auto main()
-> int
{ using my::f; f( g, 1, 2, 3, 4, 5, 6 ); }
Cheers!,
- Alf