2

たとえば、展開しているパラメーター パックがあるとします。

template<typename... P> void f(P...&& args) {
    some_other_func(std::forward<P>(args)...);
}

ここで、これらのオブジェクトが通過する必要のある他の小さな機能があるとしましょう。

template<typename T> T&& some_func(T&& ref) {
    // replace with actual logic
    return std::forward<T>(ref);
}

私は通常、単に

template<typename... P> void f(P...&& args) {
    some_other_func(some_func(args)...);
}

some_funcしかし、パラメータ パック内の数値的な位置など、パラメータのタイプだけでなく、パラメータに関する詳細情報が必要な場合はどうすればよいでしょうか。そのため、拡張する代わりに

some_other_func(some_func(arg1), some_func(arg2));

私はそれを展開することができました

some_other_func(some_func(arg1, 1), some_func(arg2, 2));

例えば?

4

2 に答える 2

2

少し複雑です。ただし、これは、 <__tuple>および<tuple>にあるlibc++のいくつかのプライベートユーティリティを使用したコードの実用的なプロトタイプです。

#include <iostream>
#include <tuple>

template<typename T>
int
some_func(T&& ref, size_t I)
{
    std::cout << "ref = " << ref << ", I = " << I << '\n';
    return 0;
}

template<typename... T, size_t ...Indx>
void
some_other_func(std::tuple<T...> ref, std::__tuple_indices<Indx...>) {
    // replace with actual logic
    std::__swallow(some_func(std::get<Indx>(ref), Indx)...);
}


template<typename... P>
void
f(P&&... args)
{
    some_other_func(std::forward_as_tuple<P...>(std::forward<P>(args)...),
                    typename std::__make_tuple_indices<sizeof...(P)>::type());
}

int main()
{
    f("zero", "one", "two", "three");
}

ref = zero, I = 0
ref = one, I = 1
ref = two, I = 2
ref = three, I = 3
于 2011-04-25T01:54:42.803 に答える
2

私は以前にこれを解決したことを知っていますが、その方法を思い出せません。まあ、これが新鮮な外観です。

数列は、を使用して引数シーケンスに変換できるstd::getため、より基本的です。したがって、ある種のカスタムツールを実装する必要があると仮定すると、ナンバーパックジェネレーターは良い選択のように思えます。

(ああ、これは信じられないほど退屈でした。ハワードの答えを覗いて学んだのですforward_as_tupleが、その関数は私のコンパイラーやideone.comにもまだ存在していないので、何とか。まだまっすぐにする必要があることがたくさんあります。 、そしてこれは確かにこれまでに発明された中で最悪の関数型言語の1つです。)

http://ideone.com/u5noV

#include <tuple>

// Generic pack array (metacontainer)
template< typename T, T ... seq > struct value_sequence {
    // Append a value to the array (metafunction)
    template< T val > struct append
        { typedef value_sequence< T, seq..., val > type; };
};

// Generate a sequential array (metafunction)
template< size_t N >
struct index_sequence {
    typedef typename index_sequence< N - 1 >::type
                      ::template append< N - 1 >::type type;
};

template<>
struct index_sequence< 0 >
    { typedef value_sequence< size_t > type; };

// Generate indexes up to size of given tuple (metafunction)
template< typename T >
struct index_tuple {
    typedef typename index_sequence< std::tuple_size< T >::value
                                   >::type type;
};

// The magic function: passes indexes, makes all the function calls
template< typename F, typename G,
          typename T, size_t ... N >
void compose_with_indexes_helper( F f, G g, T args,
        value_sequence< size_t, N ... > ) {
    f( g( std::get< N >( args ), N ) ... );
}

template< typename F, typename G, typename ... T >
void compose_with_indexes( F f, G g, T && ... args ) {
    typedef std::tuple< T && ... > tuple_t;
    compose_with_indexes_helper
//        forwarding seems broken on ideone.com/GCC 4.5.1, work around.
//        ( f, g, std::forward_as_tuple( std::forward( args ) ... ) );
        ( f, g, tuple_t( args ... ), typename index_tuple< tuple_t >::type() );
}
于 2011-04-26T11:01:01.733 に答える