Question: I have a vector class that has addition, multiplication, subtraction, division, etc operators. I'm trying to optimize my program (which does a lot of vector operations) and I'm realizing that about 50% of the time spent is in constructing and destructing vectors. I understand that this is because every time I call a non-compound mathematical operator for a vector, a new vector is constructed. Is there a way to prevent this without using compound operators or expanding vector operations?

If I have:

```
Vector a = Vector(x, y, z);
Vector b = Vector(a, b, c);

Vector c = a + b;

```

I can't use += because c is a completely new vector. I know I can speed it up with this:

```
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;

```

but that doesn't seem as clean as just using an operator.

Answer: 
> 
> I understand that this is because
>  every time I call a non-compound
>  mathematical operator for a vector, a
>  new vector is constructed. Is there a
>  way to prevent this without using
>  compound operators or expanding vector
>  operations?
> 
> 
> 

Well, the nature of adding two things together to produce a third thing *requires* you to construct a third thing...so how is what you're asking even logically possible?

That being said, if you're concerned about the temporaries created by using the addition operator, these may be optimized out by the compiler if your compiler supports return value optimization. Alternatively, if your compiler doesn't support this and you really want to cut back on temporaries but keep the `+` operator, you may want to look into emulating C++0x move semantics, and provide your vector with a rvalue move constructor, which will be invoked when it returns a temporary by value. See the Section titled "Moving Objects" in [this article](http://www.ddj.com/cpp/184403806) for information on implementing move semantics in C++03. Once C++0x comes out, you can just replace these hacks with real move constructors using the `&&` operator for rvalue references.
Make sure you have optimizations turned on and that your compiler is applying [RVO](http://en.wikipedia.org/wiki/Return_value_optimization), which is designed for exactly this situation (but not required to be used). (You may have to use a form of NRVO in your op+ implementation, example below, which helps the compiler recognize and apply RVO.) Also, have you looked at [blitz++](http://www.oonumerics.org/blitz/)?

```
Vector operator+(Vector const& a, Vector const& b) {
  Vector nrvo;
  // or: Vector nrvo (ctor, parameters, here);
  //...
  return nrvo;
}

```

---

There are other alternatives too, such as doing an explicit copy and then using op+=, which serves in a situation where RVO doesn't apply:

```
Vector c; // created somewhere else, and we want to assign to it
// instead of create it "in-place" as RVO does

Vector a, b;
// instead of:
//c = a + b
// use:
c = a;
c += b;

```

This can be achieved with [expression templates](http://en.wikipedia.org/wiki/Expression_templates), like blitz++ uses, without changing your syntax from `c = a + b`.