#include <iostream>
#include <typeinfo>
usingnamespace std;
int main(){
typedefchar,int,string,float,double var;
var a=1;
var b=2;
var c=a+b;
cout<<c<<endl;
var word="Hello";
cout<<word;
return 0;}
Not only is there no real way to do this in the current version of C++, but even when C++0x comes around C++ will remain a strongly-typed language. The addition of auto won't change that. Sorry. :)
How is the compiler to know if a should be treated as a character or an integer or a float or a double? I don't know what to treat it as and I've got the benefit of context.
They look like valid char values as well as ints. I could interpret
var a=1
as var is an integer of value 1, var is the character '1', or var is the character that is stored in memory as the number 1. If I take them as chars, I could then take the + as concatenation, except that I know people sometimes add a fixed integer value to a character to change is from upper/lower case to lower/upper case, so I'm really just guessing at that point, and we're one step away from the clusterf that is javascript and the fiasco where '5' + 3 gives '53' but '5' - 3 gives 2.
On a second note, you can actually do something like that with tagged unions... just that of course you would have to implement code that checks which var type we have there, and picks the corresponding operation.
@Moschops: I wasn't aware we had any char or string type that implements the + operator.
@Hanst: std::string uses the '+' operator for concatenation. Additionally, I could overload it at any time and add a shedload of my own such operations.