0

Assuming a std::map<T, std::size_t>, what is the result of the insertion below, assuming x does not exist yet ? unspecified, undefined behavior or well known result ?

map[x] = map.size();

I am lacking of imagination to summarize the question in the title, if someone has a better idea for it, please update, thanks.

To my comprehension of c++, the sequence point is only set at the ;, so a compiler can do whatever he wants and it should be unspecified behavior, but would appreciate if someone can pin point the standard quote on it.

galop1n
  • 8,573
  • 22
  • 36
  • 4
    You basically need someone to do one of those long sequenced-before-after-in-whatever answers. Or can't you find a general-purpose one that tells you what happens on the LHS vs RHS of an `op=` for a class type w.r.t. evaluation order? – Lightness Races in Orbit Dec 16 '14 at 00:48
  • Hint: first the rhs is evaluated, then it is assigned to the lhs. – Stefano Sanfilippo Dec 16 '14 at 00:49
  • 1
    @StefanoSanfilippo: You would need to prove that with standard language in order to be convincing, because it's often not that simple at all. When is the LHS evaluated? That's what you want to be asking. – Lightness Races in Orbit Dec 16 '14 at 00:49
  • 1
    The `=` is the built-in operator, and the value computations of the two sides of it are unsequenced - except that both are function calls, so they are indeterminately sequenced instead. Unspecified result - the `size()` can happen either before or after the element is inserted. – T.C. Dec 16 '14 at 00:52
  • 3
    Dupe is close enough IMO – Lightness Races in Orbit Dec 16 '14 at 00:53
  • I agree on the duplicate – galop1n Dec 16 '14 at 00:54
  • NOte that "sequence point" is deprecated in favor of 'sequenced before' and 'sequenced after'. And "assignation" is the most creative way I've heard of describing "assignment" –  Dec 16 '14 at 00:55
  • 2
    @LightnessRacesinOrbit You are not going to edit the question and then open it back again for the hat, are you? – Shoe Dec 16 '14 at 00:55
  • @Jefffrey Don't give me ideas ;p – Lightness Races in Orbit Dec 16 '14 at 11:09

0 Answers0