The ext::parse function template provides mapping of type to the most suitable C standard library conversion function. The std::string and std::wstring versions are available for all intrinsic types.
template <typename T>
T ext::parse (const std::string &);
template <typename T>
T ext::parse (const std::wstring &);
// specializations follows
template <> bool parse <bool> (const std::string &);
template <> bool parse <bool> (const std::wstring &);
template <> char parse <char> (const std::string &);
template <> char parse <char> (const std::wstring &);
template <> wchar_t parse <wchar_t> (const std::string &);
template <> wchar_t parse <wchar_t> (const std::wstring &);
template <> signed char parse <signed char> (const std::string &);
template <> signed char parse <signed char> (const std::wstring &);
template <> signed short parse <signed short> (const std::string &);
template <> signed short parse <signed short> (const std::wstring &);
template <> signed int parse <signed int> (const std::string &);
template <> signed int parse <signed int> (const std::wstring &);
template <> signed long parse <signed long> (const std::string &);
template <> signed long parse <signed long> (const std::wstring &);
template <> signed long long parse <signed long long> (const std::string &);
template <> signed long long parse <signed long long> (const std::wstring &);
template <> unsigned char parse <unsigned char> (const std::string &);
template <> unsigned char parse <unsigned char> (const std::wstring &);
template <> unsigned short parse <unsigned short> (const std::string &);
template <> unsigned short parse <unsigned short> (const std::wstring &);
template <> unsigned int parse <unsigned int> (const std::string &);
template <> unsigned int parse <unsigned int> (const std::wstring &);
template <> unsigned long parse <unsigned long> (const std::string &);
template <> unsigned long parse <unsigned long> (const std::wstring &);
template <> unsigned long long parse <unsigned long long> (const std::string &);
template <> unsigned long long parse <unsigned long long> (const std::wstring &);
template <> float parse <float> (const std::string &);
template <> float parse <float> (const std::wstring &);
template <> double parse <double> (const std::string &);
template <> double parse <double> (const std::wstring &);
template <> long double parse <long double> (const std::string &);
template <> long double parse <long double> (const std::wstring &);
Parsing of bool type follows these rules:
When converting to char or wchar_t, the first character of the string is used if any. The ext::common_echo is used for conversion if the character-type of the string and the target type are not the same.
Otherwise the appropriate C library conversion function is used:
#include <ext/c++>
template <typename T>
T ask_for () {
char tmp [64];
std::fgets (tmp, 64, stdin);
return ext::parse <T> (tmp);
};
int main () {
std::printf ("Enter count: ");
unsigned int n = ask_for <unsigned int> ();
std::printf ("Enter value: ");
float f = ask_for <float> ();
// ...
return 0;
};
Note that the declarations presented in interface section above is actually expanded using preprocessor macros in header file.
Due to automatic conversion from string literals to appropriate std::string or std::wstring you can also write expressions like: ext::parse <T> ("123.456");
[index]