I tried to raise stack overflow using template like the following :
#include <iostream>
using namespace std;
#define endl '\n'
template <class T>
// void next (T a) cout << a++ << endl; // can't write in a line without {}
void next (T a)
{
if (typeid(a) == typeid((char) 'a') || typeid(a) == typeid((unsigned char) 'a'))
{
cout << typeid(a).name() << " : " << (int) a << " + 1 = " << (int) ++a << " (converted to ASCII value)" << endl;
} else
{
cout << typeid(a).name() << " : " << a << " + 1 = " << ++a << endl;
}
// there will be more alternatives like type_info and so on ……
}
int main()
{
next((char) CHAR_MAX);
next((unsigned char) UCHAR_MAX);
next((short) SHRT_MAX);
next((unsigned short) USHRT_MAX);
next((int) INT_MAX);
next((unsigned int) UINT_MAX);
next((bool) 1); // warning: use of an operand of type 'bool' in 'operator++' is deprecated
return 0;
}
- results :
c : 127 + 1 = -128 (converted to ASCII value)
h : 255 + 1 = 0 (converted to ASCII value)
s : 32767 + 1 = -32768
t : 65535 + 1 = 0
i : 2147483647 + 1 = -2147483648
j : 4294967295 + 1 = 0
b : 1 + 1 = 1
This is an advanced code from my previous one using overloading similar functions for each data type(so shame, so it should be secret).
But now I have more question if I can compress the series of next()
in main()
more. I think it seems to require a container that can have various typed data; for example, {short 1, int 10, long long 100}.
Thank you for your advices & above all, take care of your health.