c++undefined-behaviorboost-foreach

Does this cause undefined behavior?


Address sanitizer is complaining.

struct X
{
  iterator begin();
  iterator end();
};

X foo();

const X& bar(const X& x)
{
  return x;
}

BOOST_FOREACH(const auto& xitem, bar(foo()))
{
  //use xitem
}

Solution

  • Yes. The FOREACH is happening on a reference to a struct which has been created by the foo call, and gone out of scope after being passed through to bar()