Sum of array elements as constexpr
Asked Answered
C

5

8

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?

Codd answered 14/2, 2017 at 6:19 Comment(3)
does it become 1M lines of code when array is 1M elements? Or does it simply become a self-repeating function?Punjab
At some point it might be easier to use a code generator than to go down this road of strange hackery.Ytterbia
You can achieve this in class template context (example).Erigeron
D
4

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);
}
Dichlorodifluoromethane answered 14/2, 2017 at 6:27 Comment(1)
Size can even be deduced.Lovelady
S
5

+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

Scharaga answered 14/2, 2017 at 10:41 Comment(0)
D
4

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);
}
Dichlorodifluoromethane answered 14/2, 2017 at 6:27 Comment(1)
Size can even be deduced.Lovelady
T
4

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)];
Ticking answered 14/2, 2017 at 6:42 Comment(0)
M
1
//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

Maryammaryann answered 14/2, 2017 at 6:42 Comment(0)
E
1

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, "!");
Enrol answered 14/2, 2017 at 19:27 Comment(4)
The depth of the recursion doesn't matter, as it will be computed at compile time. Moreover, even when calling at run-time, the compiler will most likely un-roll the recursion anyway.Barbiebarbieri
I mean recursion on compile time. compare these: rextester.com/MWJ92273 and rextester.com/VWDEWT41943Enrol
Not to mention that -ftemplate-depth=n defaults to n=900, meaning the compiler will only allow recursion 900 levels deep. If you want to sum a constexpr array bigger than size 900 and don't care about the absurd compilation time, you would have to manually set this flag if you don't use a O(log(n)) method like @KhurshidNormuradovDoodle
-ftemplate-depth=n dont help for big projects, when used a lot of template algorithms, like boost.MSM (finite state machine), boost.MPL, I everytime prefer O(log(n)) compile time algoirthms than manual set to flag.Enrol

© 2022 - 2024 — McMap. All rights reserved.