int& funA(int &a, int &b) { if(a<b) return a; else return b; }
Poprawne ale wyjątkowo głupie.
int funB(int &a, int &b) { if(a<b) return a; else return b; }
Poprawne ale nadal głupie.
int funC(int a, int b) { if(a<b) return a; else return b; }
Poprawne i bez głupot.
int& funB(int a, int b) { if(a<b) return a; else return b; }
UB
Na boku warto dodać, że zwracany przez return
obiekt lokalny z funkcji nadal można złapać przez "referencję" (const l-value ref/r-value ref) ale ten przypadek jest wyjątkiem paragrafu 12.5. Oznacza to, że nie powoduje to wydłużenia czasu życia zwracanego obiektu do czasu życia referencji, jak ma to miejsce w innych przypadkach:
The second context is when a reference is bound to a temporary.118 The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:
(...)
— The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not extended; the temporary is destroyed at the end of the full-expression in the return statement.