At first I thought this was a C++ question for some reason!
If you can use C++ you might be able to use an STL array (where the size is a template parameter, not a stored value).
It might look something like this:
std::array<double, 7> a = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0};
std::array<double, 7> b = {0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0};
auto c = a + b;
auto d = a + b + c;
And you can define operator overload for operator+. E.g:
#include <array>
// ...
// (version 1) returns a new copy/temporary
template <class T, size_t size>
std::array<T, size> operator+(const std::array<T, size>& a,
const std::array<T, size>& b)
{
std::array<T, size> c;
// add them however you want; simple version:
for (size_t i = 0; i < size; ++i)
{
c[i] = a[i] + b[i];
}
return c;
}
// (version 2) no temporaries via rvalue ref/move semantic
template <class T, size_t size>
std::array<T, size>&& operator+(std::array<T, size>&& a,
const std::array<T, size>& b)
{
for (size_t i = 0; i < size; ++i)
{
a[i] += b[i];
}
return std::move(a);
}
so that:
auto c = a + b; // invokes version 1
auto d = a + b + c; // invokes version 1 for (b+c)
// and then version 2 for a+(temp)
so you use at most one temporary in any chain of operations.
The memory layout of a std::array should be the same as a native array, so you should be able to "inject" this code quite easily into your existing program (by (ab)using casts, typedefs or preprocessor) if you want to touch a minimal amount of existing code.