#include <iostream>template <typename T>constexpr T even_sum(T seq[], int n, int i, T sum) { return i == n || seq[i] % 2 != 0 ? sum : even_sum(seq, n, i+1, sum + seq[i]);}int main() { constexpr int sequence[] = {4, 6, 8, 10}; constexpr int n = sizeof(sequence) / sizeof(*sequence); constexpr int k = even_sum(const_cast<int *>(sequence), n, 0, 0); std::cout << k << std::endl; return 0;}
Использован тернарный оператор.
Код:
#include <iostream>template <typename T>constexpr T even_sum(T seq[], int n, int i, T sum) { return i == n || seq[i] % 2 != 0 ? sum : even_sum(seq, n, i+1, sum + seq[i]);}int main() { constexpr int sequence[] = {4, 6, 8, 10}; constexpr int n = sizeof(sequence) / sizeof(*sequence); constexpr int k = even_sum(const_cast<int *>(sequence), n, 0, 0); std::cout << k << std::endl; return 0;}