1
template <typename dataTypeA, 
          typename dataTypeB> 
                             dataTypeB const& functionX (dataTypeA argA, 
                                                         dataTypeB const& argB)
{
    return argA;
}

int main ()
{
    cout << functionX (3, 1L);
    return 0;
}

The compilation:

anisha@linux-dopx:~/Desktop/notes/c++> g++ functionTemplates.cpp -Wall -Wextra -pedantic

functionTemplates.cpp: In function ‘const dataTypeB& functionX(dataTypeA, const dataTypeB&) [with dataTypeA = int, dataTypeB = long int]’:
functionTemplates.cpp:47:26:   instantiated from here
functionTemplates.cpp:35:9: warning: returning reference to temporary

and then:

anisha@linux-dopx:~/Desktop/notes/c++> ./a.out
3

Why is it returning 3?

Isn't argA a local variable for that function? Returning its reference shouldn't be successful, isn't it?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • 1
    It's a good idea to run your program through valgrind, which will tell you that something is wrong. A very short test program doesn't always make an error evident. – Kerrek SB Aug 27 '11 at 10:18

2 Answers2

5

The compiler issues a warning, that you are returning an reference to local variable.

It works because returning a reference to local variable from a function is Undefined Behavior.
Undefined Behavior means anything can happen and the behavior cannot be explained within the semantics of the C++ Standard.

You are just being lucky, rather unlucky that it works. It may not work always.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 2
    Compiler warnings should not be overlooked – Nikko Aug 27 '11 at 09:40
  • Thanks, I thought so too, but then if the local variable does NOT exist outside its scope, it shouldn't be accessible AT ALL, IMO. – Aquarius_Girl Aug 27 '11 at 09:40
  • @Anisha Kaul: There was a very famous question around here somewhere, which talked of this, let me find it for you. – Alok Save Aug 27 '11 at 09:42
  • It isn't *accessible*, you're breaking the rules by trying to make it so. – Flexo Aug 27 '11 at 09:43
  • 1
    @Anisha Kaul: [Here](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope/6445794#6445794) you go. – Alok Save Aug 27 '11 at 09:44
  • Thanks Als, but awoodland linked the same page, before you. ;) BTW, writing simply @Anisha will do, no need to write the lengthy name :) – Aquarius_Girl Aug 27 '11 at 09:47
  • @Anisha: Ah okay :) Being truly pedantic I talked about existence of the Q before awoodland posted the answer, it just took me a lil while to search the link, Anyhow most of us regular folks know about that Q quite well because of the tremendous upvotes & popularity it received. ;-) – Alok Save Aug 27 '11 at 09:51
1

You're returning a reference to the copy of argA, as it existed when you called the function. When you return from that function that copy will have been destroyed and the space it was in can quite legitimately be used by something else.

This is no different to this question, except that you're using a reference instead of a pointer.

Community
  • 1
  • 1
Flexo
  • 87,323
  • 22
  • 191
  • 272