Is there a standard library function to check that a string S is of type T and therefore can be converted to a variable of type T?
I know there is istringstream STL class that can, using operator>>, fill a variable of type T with a value converted from string. However, it will be filled with nonesense if the string content did not have the format of type T.
The best you can do is try and fail, as @Cameron commented:
Or, without boost:
Usage:
First version:
Second version:
Note that
boost::lexical_castis basically a cleverer version of this, which claims to be very efficient (potentially moreso than using iostreams unconditionally as we did here).