I have a code like this in C++:
#include <iostream>
#include <deque>
#include <type_traits>
using namespace std;
int main()
{
deque<int*> myDeque;
int a = 10, b = 20;
myDeque.push_front(&b);
myDeque.push_back(&a);
int *const &r = myDeque.back();
myDeque.pop_back();
cout<<is_reference<decltype(r)>::value<<endl;
cout << r << endl;
cout << *r << endl;
return 0;
}
Basically what I did is using a reference to the previously last element in the deque that has been deleted. However, the compiler reports no error and the behavior is not undefined. Could someone explain to me why this works? Thanks in advance!