Today I came across some code that exhibits different behavior on clang++ (3.7-git), g++ (4.9.2) and Visual Studio 2013. After some reduction I came up with this snippet which h
Well, no, it's not a case of undefined behaviour. It is a case of unspecified behaviour.
It is unspecified whether the expression len_ = len
will be evaluated before or after buffer(len+1)
. From the output you have described, g++ evaluates buffer(len+1)
first, and clang evaluates len_ = len
first.
Both possibilities are correct, since the order of evaluation of those two sub-expressions is unspecified . Both expressions will be evaluated (so the behaviour does not qualify as being undefined) but the standard does not specify the order.