dimanche 5 avril 2020

Tuple index out of bounds

I am trying to use Recursive Templates for calculating the sum of elements in a tuple. But i keep running into an error for tuple index out of bounds. error C2338: tuple index out of bounds

template <typename T, typename Tuple, std::size_t N>
struct Calculator
{
    static T summation(const Tuple& pack) 
    {
       T packValue = get<N>(pack);
       return packValue + Calculator<T, Tuple, N - 1>::summation(pack);
    }
};

template<typename T, typename Tuple>
struct Calculator<T, Tuple, 1>
{
    static T summation(const Tuple& pack)
   {
       return get<0>(pack);
   }
};



int main()
{

    tuple<double, double> t1 = make_tuple(16565.256, 45.539);

    Calculator<double, tuple<double, double>, 2>::summation(t1);
    cout << "Total Sum is: " << Calculator<double, tuple<double, double>, 2>::summation << endl;


}

How can i go about fixing the tuples index out of bounds error? I thought that having the end condition for the partially specialized Template Calculator with size_t as 1 should suffice, but the recursive tuple keeps going beyond the actual number of elements in the tuple.

Aucun commentaire:

Enregistrer un commentaire