2016-04-27 5 views
0
template <class... T_values> 
class Thing { 
public: 
    void something(T_values... values) { 
    tuple_ = std::tuple<T_values...>(values...); 
    } 

    void do_something_with_values() { 
    call_yadda_with_tuple(tuple_, 
     std::index_sequence_for<T_value...>()) 
    } 

    void yadda(T... values); 

private: 
    //The helper method. 
    template<std::size_t... Is> 
    void call_yadda_with_tuple(const std::tuple<T_values...>& tuple, 
    std::index_sequence<Is...>) { 
    yadda(std::get<Is>(tuple)...); 
    } 

    std::tuple<T_values...> tuple_; 
}; 

はから来ているいくつかの問題:https://www.murrayc.com/permalink/2015/12/05/modern-c-variadic-template-parameters-and-tuples/バリデーショナルテンプレート。ソースコード上

私はいくつかの質問をしたいと思います:

  1. std::index_sequence_for<T_value...>())を返すのですか?
  2. なぜyadda(std::get<Is>(tuple)...);Is...の代わりにIsがありますか?したがって、Isとはどういう意味ですか? Is...はパックされていない(展開された)タイプパックですが、Isとは何ですか? std::getから嵌合特に
  3. 、(1) - (8) (http://en.cppreference.com/w/cpp/utility/tuple/get
  4. call_yadda_with_tuplestd::index_sequence<Is...>を取得なぜ。結局のところ、この議論は無名なので役に立たない。私はそれが控除の種類に関連していると思いますが、どのように役立つのか分かりません。
+0

#2 'のstd :: (タプル)を取得します。.. 。'' ... 'の値ごとに式全体をアンパックします。 – kfsone

+0

#3は1-4の形式です(つまり、 'get <0>(tuple)'はタプルの最初のエントリを返します 'get <1>(タプル)' 2番目のように返されます...)テンプレート引数は、タプルエントリに戻り値を返す数値インデックスです。注:これはコンパイル時です。 – Nim

+0

#4関数自体の引数は冗長です。インデックスシーケンスはここでは関連しています。ここでは数字のシーケンス '0,1,2,3'など) – Nim

答えて

2

std :: index_sequence_for()を返すのは何ですか? T値を想定

... T、T、T(すなわち、3種類の...)

std::index_sequence<0, 1, 2> 

なぜyaddaで(STD :: ...(タプル)を取得)です。 Isの代わりにIs ...がありますか?したがって、それは何を意味していますか? ...展開された(展開された)タイプのパックではなく、Isとは何ですか?

IsIs...が展開されている間、「あるの現在の値」を表しています。末尾の...は、Isが使用されている式のアンパックを行います。 std

特に

、::(1)から適合を取得 - タプル参照がconst std::tuple<T_values...>&である。この場合、それは、数3

だろう(8)( http://en.cppreference.com/w/cpp/utility/tuple/get

なぜcall_yadda_with_tupleがstd :: index_sequenceを取得するのですか。結局のところ、この議論は無名なので役に立たない。私はそれが控除の種類に関連していると思いますが、どのように役立つのか分かりません。

それはIs...が存在し、したがって、あなたが順番にすべてのIs全体で拡大することを可能にさせるだけであります。

編集:

はここでうまくいけば

#include <utility> 
#include <tuple> 
#include <string> 
#include <iostream> 

// for any value I, write a comma and space to stdout 
// (i.e. ignore the value I) 
template<std::size_t I> 
void emit_sep() 
{ 
    std::cout << ", "; 
} 

// specialise for when I is zero... no comma in this case 
template<> 
void emit_sep<0>() 
{ 
} 

// emit and value at some position I. Use emit_sep<I> to determine whether 
// to print a separator 
template<std::size_t I, class T> 
void emit(const T& t) 
{ 
    emit_sep<I>(); 
    std::cout << t; 
} 

// given a tuple type and a sequence of integers (Is...) emit the value 
// at each index position of the tuple. Take care to emit a separator only 
// before each element after the first one 
template<class Tuple, size_t...Is> 
void impl_show_it(const Tuple& tup, std::index_sequence<Is...>) 
{ 
    using expand = int[]; 

    std::cout << "here are the indexes in the index_sequence: "; 
    // the following line will expand (in our example) to: 
    // void(int[] { 0, 
    //  (emit<0>(0), 0), 
    //  (emit<1>(1), 0), 
    //  (emit<2>(2), 0), 
    //  }); 
    // and the optimiser will remove the operations which have no observable 
    // side-effects (namely, building an array of ints which is never used) 
    // so the code emitted will be equivalent to: 
    // emit<0>(0); emit<1>(1); emit<2>(2); 
    // 
    void(expand { 
     0, 
     (emit<Is>(Is), 0)... 
    }); 
    std::cout << std::endl; 

    std::cout << "here are the values in the tuple: "; 
    void(expand { 
     0, 
     (emit<Is>(std::get<Is>(tup)), 0)... 
    }); 
    std::cout << std::endl; 
} 

// for some tuple type, compute the size of the tuple, build an index sequence 
// representing each INDEX in the tuple and then use that sequence to call 
// impl_show_it in order to actually perform the write 
template<class Tuple> 
void show_it(const Tuple& tup) 
{ 
    constexpr auto tuple_size = std::tuple_size<Tuple>::value; 
    auto sequence = std::make_index_sequence<tuple_size>(); 
    impl_show_it(tup, sequence); 
} 

// make a tuple and then show it on stdout 
int main() 
{ 
    auto t = std::make_tuple(6, std::string("hello"), 5.5); 
    show_it(t); 
} 

期待される結果に何が起こっているのかを説明するコメントを持つ例を示します

here are the indexes in the index_sequence: 0, 1, 2 
here are the values in the tuple: 6, hello, 5.5 
+0

#3彼はあなたにリンクに従うように頼んでいますリストされているstd :: getの8種類のうちどれが呼び出されたものかを教えてください – kfsone

+0

@kfsoneは答えを更新しました。ありがとうございました。 –

+0

Isは、Is ...がアンパックされている間に 'Isの現在の値'を表します。末尾の...は、Isが使用されている式のアンパックを行います。 まだ分かりません。もっと詳しく説明してください:) –