Template trick to optimize out allocations

Posted by anon on Stack Overflow See other posts from Stack Overflow or by anon
Published on 2010-12-24T16:51:46Z Indexed on 2010/12/24 16:54 UTC
Read the original article Hit count: 227

Filed under:

I have:

struct DoubleVec {
  std::vector<double> data;
};

DoubleVec operator+(const DoubleVec& lhs, const DoubleVec& rhs) {
  DoubleVec ans(lhs.size());
  for(int i = 0; i < lhs.size(); ++i) {
    ans[i] = lhs[i]] + rhs[i]; // assume lhs.size() == rhs.size()
  }
  return ans;
}

DoubleVec someFunc(DoubleVec a, DoubleVec b, DoubleVec c, DoubleVec d) {
  DoubleVec ans = a + b + c + d;
}

Now, in the above, the "a + b + c + d" will cause the creation of 3 temporary DoubleVec's -- is there a way to optimize this away with some type of template magic ... i.e. to optimize it down to something equivalent to:

DoubleVec ans(a.size());
for(int i = 0; i < ans.size(); i++) ans[i] = a[i] + b[i] + c[i] + d[i];

You can assume all DoubleVec's have the same # of elements.

The high level idea is to have do some type of templateied magic on "+", which "delays the computation" until the =, at which point it looks into itself, goes hmm ... I'm just adding thes numbers, and syntheizes a[i] + b[i] + c[i] + d[i] ... instead of all the temporaries.

Thanks!

© Stack Overflow or respective owner

Related posts about c++