Handy C++11 function similar to lexical_cast with some added support for
scientific notation, octal, and hexadecimal. The main focus of lexical_eval
is correctly casting std::string objects into C++ fundamental types (like
bool, int, char) according to rules normally honored by the C++ scanner.
Some examples:
lexical_eval<int>("3")= 3lexical_eval<int>("0xFF")= 255lexical_eval<int>("0777")= 511
Copy include/lexical_eval.hpp into your include path and just include where
you'd like to use it.
#include <iostream>
#include <string>
#include <lexical_eval.hpp>
int main() {
std::string s;
std::cout << "Enter a number: ";
std::cin >> s;
std::cout << "As integer: " << lexical_eval<int>(s) << std::endl;
std::cout << "As floating point: " << lexical_eval<float>(s) << std::endl;
}
lexical_evalassumes you probably would like the numeric data over the character data, so calls to it withstd::stringas the source andcharas the target follow this rule, which is not usual for stream semantics. There are better ways to extract character data from strings; this is not one of them.- Currently
lexical_evalis not very safe, as no exceptions are thrown for bad inputs, so use judiciously.