# SVector addition operator?

**URL:** <https://root-forum.cern.ch/t/svector-addition-operator/38614>\
**Category:** ROOT\
**Tags:** math\
**Created:** [March 30, 2020, 6:50am UTC](https://root-forum.cern.ch/t/svector-addition-operator/38614 "2020-03-30T06:50:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Brown](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@Dave\_Brown](https://root-forum.cern.ch/u/Dave_Brown)\
**Post date:** [March 30, 2020, 6:50am UTC](https://root-forum.cern.ch/t/svector-addition-operator/38614/1 "2020-03-30T06:50:57Z")

</div>

I’m using SVector and SMatrix classes to do vector algebra instead of CLHEP vectors and matrices in my new project. In general I am happy with them, but I ran into a curious problem when I wrote something like the following: SVector a,b,c; c = a + b; That code compiles and ‘runs’, even though SVector documentation and SVector.h don’t mention operator +. The call produces a resultant object ‘c’ object without numerical payload, just a pair of references to ‘a’ and ‘b’. I’m wondering if this is an undocumented feature of SVector, or if the compiler just invented this. I get the same results using both gnu g++ and clang.

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

_ROOT Version:_ Not Provided  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [March 30, 2020, 6:54am UTC](https://root-forum.cern.ch/t/svector-addition-operator/38614/2 "2020-03-30T06:54:35Z")

</div>

I think @moneta can give an explanation about this

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 30, 2020, 7:22am UTC](https://root-forum.cern.ch/t/svector-addition-operator/38614/3 "2020-03-30T07:22:11Z")

</div>

Hi David,

The operator exists but it is implemented as a free operator , not as a member function, in the file BinaryOperations.h, see  
[https://root.cern.ch/doc/master/group\_\_VectFunction.html#ga50f00953d390e1c9696a34202244db0f](https://root.cern.ch/doc/master/group__VectFunction.html#ga50f00953d390e1c9696a34202244db0f)

Same thing is also for the SMatrix class and the other operators (-,\*,/)  
Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![Dave\_Brown](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@Dave\_Brown](https://root-forum.cern.ch/u/Dave_Brown)\
**Post date:** [March 30, 2020, 3:16pm UTC](https://root-forum.cern.ch/t/svector-addition-operator/38614/4 "2020-03-30T15:16:03Z")

</div>

Good to hear from you Lorenzo, I hope you’re well.

Thanks for the pointer to the operators, I missed that.

I ran into this because of a use case where the result of the addition had a longer lifetime than the operands, so by the time I used the result the operands had passed out of scope and the

vector expression gave erroneous values. I got around this using operator += instead. Is there a reason the binary operators return vector expressions instead of normal objects?

thanks, Dave

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 31, 2020, 12:32pm UTC](https://root-forum.cern.ch/t/svector-addition-operator/38614/5 "2020-03-31T12:32:53Z")

</div>

Hi Dave,

I am well, I hope you are too.

It is correct that in some cases the operands goes out of scope and the resulting vector expression contains references to deleted objects. This happens for example when using the `auto` keyword, see

[https://sft.its.cern.ch/jira/browse/ROOT-6371](https://sft.its.cern.ch/jira/browse/ROOT-6371)

The reason to return expressions is that operations can be combined and chained together at compiled time without creating temporary vector and matrices objects, making the computation more efficient.  
This is the basics of the design of Vector and SMatrix that are based on expression templates

Cheers,

Lorenzo

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 14, 2020, 12:32pm UTC](https://root-forum.cern.ch/t/svector-addition-operator/38614/6 "2020-04-14T12:32:53Z")

</div>

Closed on April 14, 2020, 12:32pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
