0

Is it possible to overload -= like this without it being a method of a class?

vector<int>& operator-=(int a, int b){
    vector<int> v;
    v.push_back(a); v.push_back(b);
    return v
}

I have a line in a homework assignment that looks something like this:

SomeStructure-=1-=2-=3;

What it is supposed to do is remove the elements with the indexes 1, 2 and 3 from the structure(in that order).

It seems like the option I tried above is not possible (I was thinking of collecting all the indexes in a vector and then removing them from the structure one by one). Is there some other way to do this?

nbro
  • 15,395
  • 32
  • 113
  • 196
littlerunaway
  • 443
  • 2
  • 8
  • 18
  • Can you overload by return type?????? – 101010 Sep 12 '14 at 22:02
  • I'm not sure what you mean by that – littlerunaway Sep 12 '14 at 22:02
  • I suspect you want something more like: `SomeStructure& operator-=(SomeStructure& s, int index);` – Galik Sep 12 '14 at 22:05
  • 1
    it's not up to me. I didn't choose to write it like this. out instructor did and I'm supposed to make it work somehow – littlerunaway Sep 12 '14 at 22:07
  • See http://stackoverflow.com/questions/4581961/c-how-to-overload-operator – Remy Lebeau Sep 12 '14 at 22:09
  • What @40two means is that according to your question, it looks that you can don for instance a-=b (with a and b int) and return something which is not int. Because of that, the first type of the two arguments (a) has to be the same type as the return type. How else would you distinguish between returning an int or a vector? – Javi Sep 12 '14 at 22:10
  • 1
    Plus in the code you posted you're returning a local object (i.e., `vector v`) by reference. That's undefined behaviour. – 101010 Sep 12 '14 at 22:10
  • It looks like an odd question to me, confusing two distinct issues as it does. This is your instructor's first time teaching this course, isn't it? – thb Sep 12 '14 at 22:12
  • no, but he has a habit of not thinking things through when he writes the assignments and then it's up to us to find his mistakes – littlerunaway Sep 12 '14 at 22:14

2 Answers2

4

The assignment operator is right to left associative.

From the C++ Standard

5.17 Assignment and compound assignment operators [expr.ass]

1 The assignment operator (=) and the compound assignment operators all group right-to-left.

So this expression

SomeStructure-=1-=2-=3;

is in any case invalid because you may not write

2 -= 3;

And you may not overload operators for built-in types.

I advice to write simply a function for example with name erase which will have parameter of type std::initializer_list In this case you could write

SomeStructure.erase( { 1, 2, 3 } );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • wait, but Piotr above says it is valid since `-=` has left associativity – littlerunaway Sep 12 '14 at 22:10
  • 2
    @littlerunaway: Piotr was wrong. Standard actually says "The assignment operator (=) and the compound assignment operators all group right-to-left." – Ben Voigt Sep 12 '14 at 22:11
  • so there's no way to make it work the way it's written now? because i can't change it – littlerunaway Sep 12 '14 at 22:12
  • @littlerunaway: Your question says the line is "something like" that. What is it exactly? Details could make a difference. – Ben Voigt Sep 12 '14 at 22:14
  • no, it's exactly that actually. just the structure type is something I didn't want to specify – littlerunaway Sep 12 '14 at 22:16
  • I understand there are better ways to do this, but it's not up to me – littlerunaway Sep 12 '14 at 22:20
  • 1
    @littlerunaway: We could perhaps make `some -= 1st -= 2nd -= 3rd;` work using user-defined literals... but the syntax you have now is quite impossible, since `-=` appears with two operands of built-in type, and you can't override that. – Ben Voigt Sep 12 '14 at 22:21
1

When overloading an operator, at least one of the types passed as parameter has to be a non primitive type, which is not your case, that's why we usually overload operators inside classes and more rarely as global functions.

nbro
  • 15,395
  • 32
  • 113
  • 196