#include <iostream>
using namespace std;
typedef union multytype mt;
enum types
{
NONE,
INT,
DOUBLE,
CHAR,
STRING,
CONSTSTRING
};
union multytype
{
types type;
struct { types type; int val; } ival;
struct { types type; double val; } dval;
struct { types type; char val; } cval;
struct { types type; char *val; } sval;
struct { types type; const char *val; } csval;
multytype()
{
type=NONE;
}
multytype(int val)
{
type=INT;
ival.val=val;
}
multytype(double val)
{
type=DOUBLE;
dval.val=val;
}
multytype(char val)
{
type=CHAR;
cval.val=val;
}
multytype(char* val)
{
type=STRING;
sval.val=val;
}
multytype(const char* val)
{
type=CONSTSTRING;
csval.val=val;
}
};
void print(int n, ... )
{
mt* i=(mt*)((&n)+1);
for(int j=0;j<n;j++,i++)
{
switch(i->type)
{
case INT:
cout<< i->ival.val <<endl;
break;
case DOUBLE:
cout<< i->dval.val <<endl;
break;
case CHAR:
cout<< i->cval.val <<endl;
break;
case STRING:
cout<< i->sval.val <<endl;
break;
case CONSTSTRING:
cout<< i->csval.val <<endl;
break;
}
}
}
int main()
{
char str[]="Hi";
print(5,mt(2),mt(4.2),mt('a'),mt("Hello"),mt(str));
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp0eXBlZGVmIHVuaW9uIG11bHR5dHlwZSBtdDsKZW51bSB0eXBlcwp7Ck5PTkUsCklOVCwKRE9VQkxFLApDSEFSLApTVFJJTkcsCkNPTlNUU1RSSU5HCn07CiAKdW5pb24gbXVsdHl0eXBlCnsKICAgICAgICB0eXBlcyB0eXBlOwogICAgICAgIHN0cnVjdCB7IHR5cGVzIHR5cGU7IGludCB2YWw7ICAgIH0gaXZhbDsKICAgICAgICBzdHJ1Y3QgeyB0eXBlcyB0eXBlOyBkb3VibGUgdmFsOyB9IGR2YWw7CiAgICAgICAgc3RydWN0IHsgdHlwZXMgdHlwZTsgY2hhciB2YWw7ICAgfSBjdmFsOwogICAgICAgIHN0cnVjdCB7IHR5cGVzIHR5cGU7IGNoYXIgKnZhbDsgIH0gc3ZhbDsKICAgICAgICBzdHJ1Y3QgeyB0eXBlcyB0eXBlOyBjb25zdCBjaGFyICp2YWw7ICB9IGNzdmFsOwogICAgICAgIG11bHR5dHlwZSgpCiAgICAgICAgewogICAgICAgICAgICAgICAgdHlwZT1OT05FOwogICAgICAgIH0KICAgICAgICBtdWx0eXR5cGUoaW50IHZhbCkKICAgICAgICB7CiAgICAgICAgICAgICAgICB0eXBlPUlOVDsKICAgICAgICAgICAgICAgIGl2YWwudmFsPXZhbDsKICAgICAgICB9CiAgICAgICAgbXVsdHl0eXBlKGRvdWJsZSB2YWwpCiAgICAgICAgewogICAgICAgICAgICAgICAgdHlwZT1ET1VCTEU7CiAgICAgICAgICAgICAgICBkdmFsLnZhbD12YWw7CiAgICAgICAgfQogICAgICAgIG11bHR5dHlwZShjaGFyIHZhbCkKICAgICAgICB7CiAgICAgICAgICAgICAgICB0eXBlPUNIQVI7CiAgICAgICAgICAgICAgICBjdmFsLnZhbD12YWw7CiAgICAgICAgfQogICAgICAgIG11bHR5dHlwZShjaGFyKiB2YWwpCiAgICAgICAgewogICAgICAgICAgICAgICAgdHlwZT1TVFJJTkc7CiAgICAgICAgICAgICAgICBzdmFsLnZhbD12YWw7CiAgICAgICAgfQogICAgICAgIG11bHR5dHlwZShjb25zdCBjaGFyKiB2YWwpCiAgICAgICAgewogICAgICAgICAgICAgICAgdHlwZT1DT05TVFNUUklORzsKICAgICAgICAgICAgICAgIGNzdmFsLnZhbD12YWw7CiAgICAgICAgfQp9OwogCnZvaWQgcHJpbnQoaW50IG4sIC4uLiApCnsKICAgICAgICBtdCogaT0obXQqKSgoJm4pKzEpOwogICAgICAgIGZvcihpbnQgaj0wO2o8bjtqKyssaSsrKQogICAgICAgIHsKICAgICAgICAgICAgICAgIHN3aXRjaChpLT50eXBlKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY2FzZSBJTlQ6CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PCBpLT5pdmFsLnZhbCA8PGVuZGw7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgY2FzZSBET1VCTEU6CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PCBpLT5kdmFsLnZhbCA8PGVuZGw7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgY2FzZSBDSEFSOgogICAgICAgICAgICAgICAgICAgICAgICBjb3V0PDwgaS0+Y3ZhbC52YWwgPDxlbmRsOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIGNhc2UgU1RSSU5HOgogICAgICAgICAgICAgICAgICAgICAgICBjb3V0PDwgaS0+c3ZhbC52YWwgPDxlbmRsOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIGNhc2UgQ09OU1RTVFJJTkc6CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQ8PCBpLT5jc3ZhbC52YWwgPDxlbmRsOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgIH0KICAgICAgICB9Cn0KIAppbnQgbWFpbigpCnsKICAgICAgICBjaGFyIHN0cltdPSJIaSI7CiAgICAgICAgcHJpbnQoNSxtdCgyKSxtdCg0LjIpLG10KCdhJyksbXQoIkhlbGxvIiksbXQoc3RyKSk7Cn0=