BTW, does the standard guarantee that doing this trick (int &ref = *(int *)NULL) crashes immediately? Else you could do
Code:
#include <iostream>
template <typename Type>
void swap_by_pointers(Type *a, Type *b)
{
if (a != NULL && b != NULL) {
Type temp = *a;
*a = *b;
*b = temp;
}
}
template <typename Type>
void swap(Type &a, Type &b)
{
swap_by_pointers(&a, &b);
}
int main(void)
{
int four = 4;
int &ref = *(int *)NULL;
swap(four, ref);
std::cout << "four is " << four << ". (Can't show ref)" << std::endl;
}