简体   繁体   中英

Sum of array elements as constexpr

I'm trying to get the sum of a const int array as a constexpr so that I can use sum as the size of another array

constexpr int arr[] = {1, 2, 3};
constexpr int sum1 = std::accumulate(arr, arr + 3, 0); // not OK
int arr1[sum1];

The above does not compile as std::accumulate() does not return a constexpr . I end up have a workaround like this

template <size_t N>
constexpr int sum(int const a[])
{
    return a[N-1] + sum<N - 1>(a);
}

template <>
constexpr int sum<0>(int const a[])
{
    return 0;
}

constexpr int arr[] = {1, 2, 3};
constexpr int sum1 = sum<3>(arr);
int arr1[sum1];

Is there any simpler solution?

+1 for the C++14 solutions, but I propose a C++11 solution based on a simple recursive constexpr function

template <typename T, std::size_t N>
constexpr T aSum (T const (&a)[N], std::size_t i = 0U)
 { return i < N ? (a[i] + aSum(a, i+1U)) : T{}; }

So it's possible to write

constexpr int arr[] {1, 2, 3};
constexpr int sum1 { aSum(arr) };

and sum1 become 6

Since C++14's relaxed constexpr you can do something like this:

#include <iostream>

constexpr int arr[] = {1, 2, 3};

template <size_t Size>
constexpr int sum(const int (&arr)[Size])
{
    int ret = 0;
    for (int i = 0; i < Size; ++i)
        ret += arr[i];
    return ret;
}

int main()
{
    int arr1[sum(arr)];
    std::cout << sizeof(arr1) / sizeof(int);
}

Using C++14:

template<typename T, std::size_t N>
constexpr T array_sum(T (&array)[N]) {
    T sum = 0;
    for (std::size_t i = 0; i < N; i++) {
        sum += array[i];
    }
    return sum;
};

And use it like:

int arr[] = {1, 2, 3};
int brr[array_sum(arr)];
//With C++14
#include <iostream>
using namespace std;

template <typename T, std::size_t N>
constexpr T arraysum(const T (&array)[N])  {
    T sum = 0;
    for (size_t i = 0; i < N; ++i) {
        sum += array[i];
    }
    return sum;
}

template <typename T, std::size_t N>
constexpr T arraysize(T (&)[N])  {
    return N;
}

int main() {
    // your code goes here
    constexpr int arr[] = {1, 2, 3};
    constexpr int size = arraysize(arr);
    cout<<size<<endl;
    constexpr int sum1 = arraysum(arr);
    cout <<sum1;
    int arr2[sum1];
    return 0;
}

Check code here

there a little compilacated code, but worked C++11, and required only O(log(N)) deep recursion.

template<typename Iterator >
constexpr size_t c_dist(Iterator first, Iterator last){ return (last - first); }

template< typename Iterator, typename U>
constexpr U c_accumulate(Iterator first, Iterator last, U u)
{
    return (first == last) 
            ? u 
            : c_dist(first , last) == 1 
                 ? ( u + *first ) 
                 :  
                    c_accumulate(first, first + c_dist(first, last ) / 2, u ) +  
                    c_accumulate(first + c_dist(first, last)/2, last, u);

}

constexpr int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
constexpr int sum = c_accumulate(a, a + sizeof(a)/sizeof(a[0]), 0);

static_assert(sum == 21*22/2, "!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM