Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. COThese operators are a very good example why operator overloading should be done rarely. I would have thought, `vec+=val` would _append_ a value to the vector. (See http://stackoverflow.com/questions/2551775/.) Now that I have seen your implementation, I think this is just as right an interpretation of the meaning of `+=`, too. I wouldn't know which one would be right or wrong, so it's probably just as well that we don't have `+=` for `std::vector`.
      singulars
    2. CO@sbi I agree. I find `operator+()` missing an amazing early insight of the standard. I usually expect an O(1) operation everywhere I see the plus operator. C++ makes things that are expensive or dangerous more verbose or difficult to do, and I like it this way. Take a look at Java: one of the worst coding mistakes there is the abuse of the plus operator. Of course, then again, C++ doesn't always make cheap and fast things easy, but hey. Good C++ programmers are very much performance-aware. ;)
      singulars
    3. COI agree with both of you that `op+()` shouldn't be defined at all due to its ambiguity. But vectors are usually part of an (mathematical) vector space and there is a canonical definition of adding two vectors and scalar multiplication. To take your argument further: a simple `double` is a vector as well, so if you'd add two `double` variables like `a+b` then you'd expect to get a new `double` and not a `pair` of double like `(a,b)`. Multiplying with a scalar is canonical too, but multiplying two vectors is not. So overloading should be done with care..
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload