元编程应用lexical_cast

256 篇文章 3 订阅
149 篇文章 2 订阅
#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值