我一直在尝试在通用模板和结构上使用可变参数元组。这个想法是获得可变参量的最大值和和。到目前为止,我能够获得所需的结果,但必须使用Global静态变量。我已经在实现的下面发布了代码。
#include <iomanip>
#include <complex>
#include <cmath>
#include <tuple>
#include <string>
#include <iostream>
using namespace std;
using namespace std::complex_literals;
template<typename T>
static T max;
template<typename T>
static T sum;
template<typename T>
static T average;
template <typename T, typename Tuple, std::size_t N>
struct Calculator
{
static void maximum(const Tuple& pack)
{
Calculator<T, Tuple, N - 1>::maximum(pack);
T packValue = get<N - 1>(pack);
if (packValue > max<T>)
{
//T max = get<0>(pack);
//Try the above instead of the below code to calculate max
max<T> = get<N - 1>(pack);
}
}
static void summation(const Tuple& pack)
{
Calculator<T, Tuple, N - 1>::summation(pack);
T packValue = get<N - 1>(pack);
sum<T> += packValue;
}
static void averager(const Tuple& pack)
{
average<T> = sum<T> / N;
}
};
template<typename T, typename Tuple>
struct Calculator<T, Tuple, 1>
{
static void maximum(const Tuple& pack)
{
//T max = get<0>(pack);
//Try the above instead of the below code to calculate max
max<T> = get<0>(pack);
}
static void summation(const Tuple& pack)
{
sum<T> = get<0>(pack);
}
};
int main()
{
tuple<double, double, double, double, double, double, double> t1 = make_tuple(16565.256, 45.539, 0.25, 1000.25, 1.25036, 35.66, 210.20);
Calculator<double, tuple<double, double, double, double, double, double, double>, 7>::maximum(t1);
cout << "Maximum is: " << max<double> << endl;
Calculator<double, tuple<double, double, double, double, double, double, double>, 7>::summation(t1);
cout << "Total Sum is: " << sum<double> << endl;
Calculator<double, tuple<double, double, double, double, double, double, double>, 7>::averager(t1);
cout << "Average is: " << average<double> << endl;
std::complex<int> c2(22, 3);
std::complex<int> ci(15, 41);
tuple<std::complex<int>, std::complex<int> > ct1 = make_tuple(ci, c2);
Calculator< std::complex<int>, tuple<std::complex<int>, std::complex<int> >, 2>::summation(ct1);
cout << "Summation of complex numbers is: " << sum<std::complex<int>> << endl;
}
我的问题是,是否有可能实现一个不使用Global静态变量来保存sum和max值的版本,而是在可能的情况下使用可变参数模板和结构来替代实现?
答案 0 :(得分:1)
您使用的是变量模板,因此您至少有C ++ 14。在C ++ 14中,您可以使用index_sequence
作为提取所有元组项并将重写函数重写为非递归的简便方法:
template <typename T, typename Tuple, std::size_t N>
struct Calculator
{
template<size_t ... Indices>
static T maxHelper(const Tuple& pack, std::index_sequence<Indices...>) {
return std::max( {std::get<Indices>(pack)...} );
} // [1]
static T maximum(const Tuple& pack) {
return maxHelper(pack, std::make_index_sequence<N>{});
}
template<size_t ... Indices>
static T sumHelper(const Tuple& t, std::index_sequence<Indices...>) {
T arr[] = { std::get<Indices>(t)... }; // [2]
return std::accumulate(arr,arr+N,T{}); // [3]
}
static T summation(const Tuple& pack) {
return sumHelper(pack,std::make_index_sequence<N>{});
}
};
在[1]个元组中的项被提取到std::initializer_list
中,然后调用该列表的std::max
重载。
将[2]和[3]中元组的项提取到数组中,并通过std::accumulate
进行求和。
自C ++ 17起,使用std::apply
(和 fold表达式进行求和):
template<class ... Args>
auto maxValue(std::tuple<Args...> t) {
return std::apply( [](auto&&... args) { return std::max({args...}); },t);
}
template<class ... Args>
auto sumValue(std::tuple<Args...> t) {
return std::apply( [](auto&&... args){ return (args + ...);} , t);
}