0

Does the Standard say anything about this being UB (or not) ?

int& get()
{
    int a = 1;
    return a;
}

int main()
{
   std::cout << (&get() == nullptr);
}
PoweredByRice
  • 2,479
  • 1
  • 20
  • 26
  • Any usage by the caller of the reference returned by `get()` gives undefined behaviour. Including computing the address. – Peter Oct 20 '17 at 23:01
  • Yes, the C++ Standard says this is UB. – Captain Obvlious Oct 20 '17 at 23:06
  • Answer is here : https://stackoverflow.com/questions/30618783/why-is-returning-address-of-local-variable-or-temporary-only-a-warning-and-not-a – Blood-HaZaRd Oct 20 '17 at 23:14
  • Don't really agree with this duplicate link: the code here is undefined behaviour, but the "duplicate" , in an attempt to be general, is too vague: many of the current answers say the code is OK, and they only cover a fraction of what would be a complete answer to the question – M.M Oct 20 '17 at 23:43
  • Problem with the duplicated question is no resolution. Some of the up-voted answers say it is undefined behaviour, other up-voted answers say it isn't. – Peter Oct 20 '17 at 23:59

0 Answers0