1)テンプレートファンクタN
回を実行し、2)結果を累積するバリアブルテンプレートまたは定数式を書きたいと思います。私はテンプレートファンクタに実行される関数を動かすと実際にコンパイルに失敗する小さなサンプルを書きました。私は解決策に近いと感じていますが、多分私は間違っています。コンパイルの問題を伴う単純な(再帰的)バリデーションテンプレート "accumulate_for"関数
#include <iostream>
#include <string>
struct F {
template <int id>
static int run(int val) {
return id * val;
}
};
template<unsigned int n>
struct accumulate_for
{
template <class Funct>
static int get(int val) {
return
(
accumulate_for<n-1>::get(val)
+
Funct::run<n>(val)
);
}
};
template<>
struct accumulate_for<0>
{
template <class Funct>
static int get(int val) {
return 0;
}
};
int main()
{
std::cout << accumulate_for<3>::get<F>(1) << std::endl;
}
を参照してください[STDと代替:: index_sequence](http://coliru.stacked-crooked.com/ a/a994dfd1a8890ef) – Jarod42