1

When I think of the term "pop", I think of popping something off a stack. So it is no longer on the stack, but you are in possession of it.

Tonight I was writing a class to handle temperature values and auto-convert units. I wanted one of my constructors which accepts strings such as "347.23c" or "923.40R", where if the last character value was an upper or lower case F, C, K, R value, it would be removed and used to process the calculation.

I know there are other ways to do this, that's not the issue. My question / concern is that this seems to be a deviation from the accepted definition of the POP action. Is there a reasonable argument for why this method was made void?

iammilind
  • 68,093
  • 33
  • 169
  • 336
mreff555
  • 1,049
  • 1
  • 11
  • 21
  • 1
    Popping from an `` object works that way. You use `top()` to get the top object, then you use `pop()` to remove it. That’s because the copy constructor of a user-defined type might throw an exception, and with a single operation, if the copy threw an exception there’s no reasonable way to restore the previous state. That’s what drives the design for all containers, even if the type being copied can’t throw exceptions. – Pete Becker Jul 13 '22 at 02:21
  • The reason could be that most of the containers have `pop_back()` returning `void`. However, I agree with your concern that specific to `string`, it wouldn't harm if a popped out `char` is returned by value. There might be a very tiny performance degradation due to "return mechanism" though.\ – iammilind Jul 13 '22 at 02:30

0 Answers0