#include <string.h>
#include <iostream>
#include <string>
#include <type_traits>
#include <stdexcept>
static const char *strue = "true";
static const char *sfalse = "false";
template <typename To, typename From>
struct converter {
};
template <typename From>
struct converter<int, From> {
static int convert(const From &from) {
return atoi(from);
}
};
template <typename From>
struct converter<long, From> {
static long convert(const From &from) {
return atol(from);
}
};
template <typename From>
struct converter<long long, From> {
static long long convert(const From &from) {
return atoll(from);
}
};
template <typename From>
struct converter<double, From> {
static double convert(const From &from) {
return atof(from);
}
};
template <typename From>
struct converter<float, From> {
static float convert(const From &from) {
return (float)atof(from);
}
};
template <typename From>
struct converter<bool, From> {
static typename std::enable_if<std::is_integral<From>::value, bool>::type convert(const From &from) {
return !!from;
}
};
bool check_bool(const char *from, size_t len, const char *s) {
for (auto index = 0;index < len;index++) {
if (from[index] != s[index]) {
return false;
}
}
return true;
}
static bool convert_(const char *from) {
size_t len = strlen(from);
const size_t true_len = strlen(strue);
const size_t false_len = strlen(sfalse);
if (len != true_len && len != false_len) {
throw std::invalid_argument("argument is invalid.");
}
bool r = true;
if (true_len == len) {
r = check_bool(from, len, strue);
if (true == r) {
return true;
}
}
else if (false_len == len) {
r = check_bool(from, len, sfalse);
if (true == r) {
return false;
}
}
throw std::invalid_argument("argument is invalid.");
}
template <>
struct converter<bool, std::string> {
static bool convert(const std::string &from) {
return convert_(from.c_str());
}
};
template <>
struct converter<bool, const char *> {
static bool convert(const char *from) {
return convert_(from);
}
};
template <>
struct converter<bool, char *> {
static bool convert(char *from) {
return convert_(from);
}
};
template <unsigned N>
struct converter<bool, const char[N]> {
static bool convert(const char (&from)[N]) {
return convert_(from);
}
};
template <unsigned N>
struct converter<bool, char[N]> {
static bool convert(const char (&from)[N]) {
return convert_(from);
}
};
template <typename From>
struct converter<std::string, From> {
static bool convert(const From &from) {
return std::to_string(from);
}
};
template <typename To, typename From>
typename std::enable_if<!std::is_same<To, From>::value, To>::type lexical_cast(const From &from) {
return converter<To, From>::convert(from);
}
template <typename To, typename From>
typename std::enable_if<std::is_same<To, From>::value, To>::type lexical_cast(const From &from) {
return from;
}
int main() {
std::cout << lexical_cast<int>("1.22") << std::endl;
try {
const char *str = "test";
std::cout << lexical_cast<bool>(str) << std::endl;
}
catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
return 0;
}