0

I mistakenly took the address of the reference returned by the back() operator in an empty container and was surprised to see that the address wasn't zero.

If a container e.g. std::deque is empty, what does back() return?

BeeBand
  • 10,953
  • 19
  • 61
  • 83

2 Answers2

7

it returns the last element.

on this page: http://www.sgi.com/tech/stl/BackInsertionSequence.html

precondition: !a.empty()

Equivalent to *(--a.end()).

since the precondition is the deque is not empty, then it means it's undefined behavior.

Chris H
  • 6,433
  • 5
  • 33
  • 51
  • It also means that is it most likely one element just before the "logical" place for the first element. I should say that the behavior of `front` is logically the same though the address will be the "logical" place I was talking about (for an array-like container). – Matthieu M. Feb 05 '10 at 08:05
1

Calling front or back on an empty standard container results in undefined behavior.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765