General Variable


I was wondering if there can be a general type of variable in c++, that if the input, for example, is int, that variable will become an int.
No, types must be known at compile time.
In C++17, we have std::any :

If using a compiler that doesn't support that, you can use boost::any from the boost libraries:
There is no overload of operator>> for std::any as far as I know.
Maybe auto is what you're looking for? See:
Can't you use templates to handle different types of variables?
yes, but the template has to know the type at compile time...

The trouble with this is all the guesswork.
12AB ... is it a string, or a hex number?

This sort of thing feels like a good way to make code slower and more convoluted generally. Strong types are good. Weak types are extremely difficult to work with... I spent years fighting a matlab over that, where a variable that was a string a min ago is now a double then its a matrix then its a loop counter and now its a string again...
Last edited on
Maybe auto is what you're looking for? See:

Yes this might be what I was Looking for, thanks!
Topic archived. No new replies allowed.