22 #ifndef GEARS_STRING_LEXICAL_CAST_HPP
23 #define GEARS_STRING_LEXICAL_CAST_HPP
32 template<
typename Target>
33 struct lexical_caster {
34 template<
typename CharT>
35 static inline Target cast(
const std::basic_string<CharT>& str) {
36 std::basic_istringstream<CharT> ss(str);
38 if((ss >> result).fail() || !(ss >> std::ws).eof()) {
39 throw std::invalid_argument(
"lexical_cast failed");
45 template<
typename CharT>
46 struct lexical_caster<std::basic_string<CharT>> {
47 static inline std::basic_string<CharT> cast(
const std::basic_string<CharT>& str) {
53 struct lexical_caster<int> {
54 template<
typename CharT>
55 static inline int cast(
const std::basic_string<CharT>& str) {
56 return std::stoi(str);
61 struct lexical_caster<long> {
62 template<
typename CharT>
63 static inline long cast(
const std::basic_string<CharT>& str) {
64 return std::stol(str);
70 struct lexical_caster<long long> {
71 template<
typename CharT>
72 static inline long long cast(
const std::basic_string<CharT>& str) {
73 return std::stoll(str);
78 struct lexical_caster<float> {
79 template<
typename CharT>
80 static inline float cast(
const std::basic_string<CharT>& str) {
81 return std::stof(str);
86 struct lexical_caster<double> {
87 template<
typename CharT>
88 static inline double cast(
const std::basic_string<CharT>& str) {
89 return std::stod(str);
94 struct lexical_caster<long double> {
95 template<
typename CharT>
96 static inline long double cast(
const std::basic_string<CharT>& str) {
97 return std::stold(str);
102 struct lexical_caster<unsigned long> {
103 template<
typename CharT>
104 static inline unsigned long cast(
const std::basic_string<CharT>& str) {
105 return std::stoul(str);
110 struct lexical_caster<unsigned long long> {
111 template<
typename CharT>
112 static inline unsigned long long cast(
const std::basic_string<CharT>& str) {
113 return std::stoull(str);
118 struct remove_const {
123 struct remove_const<const T> {
128 struct remove_volatile {
133 struct remove_volatile<volatile T> {
139 using type =
typename std::remove_volatile<typename std::remove_const<T>::type>::type;
159 template<
typename Target,
typename CharT>
161 return detail::lexical_caster<typename detail::remove_cv<Target>::type>::cast(str);
166 #endif // GEARS_STRING_LEXICAL_CAST_HPP