实现 boost lexical_cast(基本类型转换)

boost 中的lexical_cast专门用于基本数据类型转换 比如 int float bool double string ,但是使用时必须引入boost 库,如何自己实现一个相似的函数呢,答案便是使用到模板。

实现如下:

#include <type_traits>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <stdexcept>
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

namespace detail
{
	const char* strue = "true";
	const char* sfalse = "false";

	template <typename To, typename From>
	struct Converter
	{
	};

	//to numeric
	template <typename From>
	struct Converter<int, From>
	{
		static int convert(const From& from)
		{
			return std::atoi(from);
		}
	};

	template <typename From>
	struct Converter<long, From>
	{
		static long convert(const From& from)
		{
			return std::atol(from);
		}
	};

	template <typename From>
	struct Converter<long long, From>
	{
		static long long convert(const From& from)
		{
			return std::atoll(from);
		}
	};

	template <typename From>
	struct Converter<double, From>
	{
		static double convert(const From& from)
		{
			return std::atof(from);
		}
	};

	template <typename From>
	struct Converter<float, From>
	{
		static float convert(const From& from)
		{
			return (float)std::atof(from);
		}
	};

	//to bool
	template <typename From>
	struct Converter<bool, From>
	{
		static typename std::enable_if<std::is_integral<From>::value, bool>::type convert(From from)
		{
			return !!from;
		}
	};

	bool checkbool(const char* from, const size_t len, const char* s)
	{
		for (size_t i = 0; i < len; i++)
		{
			if (from[i] != s[i])
			{
				return false;
			}
		}

		return true;
	}

	static bool convert(const char* from)
	{
		const unsigned int len = strlen(from);
		if (len != 4 && len != 5)
			throw std::invalid_argument("argument is invalid");

		bool r = true;
		if (len == 4)
		{
			r = checkbool(from, len, strue);

			if (r)
				return true;
		}
		else
		{
			r = checkbool(from, len, sfalse);

			if (r)
				return false;
		}

		throw std::invalid_argument("argument is invalid");
	}

	template <>
	struct Converter<bool, string>
	{
		static bool convert(const string& from)
		{
			return detail::convert(from.c_str());
		}
	};

	template <>
	struct Converter<bool, const char*>
	{
		static bool convert(const char* from)
		{
			return detail::convert(from);
		}
	};

	template <>
	struct Converter<bool, char*>
	{
		static bool convert(char* from)
		{
			return detail::convert(from);
		}
	};

	template <unsigned N>
	struct Converter<bool, const char[N]>
	{
		static bool convert(const char(&from)[N])
		{
			return detail::convert(from);
		}
	};

	template <unsigned N>
	struct Converter<bool, char[N]>
	{
		static bool convert(const char(&from)[N])
		{
			return detail::convert(from);
		}
	};

	//to string
	template <typename From>
	struct Converter<string, From>
	{
		static string 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 detail::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;
}
void test()
{
	cout << lexical_cast<int>(1) << endl;
	cout << lexical_cast<int>("1") << endl;
	cout << lexical_cast<long>("1") << endl;
	cout << lexical_cast<string>(1) << endl;
	cout << lexical_cast<bool>(1) << endl;
	cout << lexical_cast<double>("1.2") << endl;
	cout << lexical_cast<float>("1.2") << endl;
	string s = "true";
	cout << lexical_cast<bool>(s) << endl;
	char* p = "false";
	cout << lexical_cast<bool>(p) << endl;
	const char* q = "false";
	cout << lexical_cast<bool>(q) << endl;
	cout << lexical_cast<bool>("false") << endl;
	cout << lexical_cast<bool>("test") << endl;
}

int main()
{
	try
	{
		test();
	}
	catch (const std::exception& e)
	{
		cout << e.what() << endl;
	}
	system("pause");
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值