c++类型推导

c++类型推导
  • C++14中,你可以使用std::is_same<T, std::string>::value来检查类型:std::is_same<T, std::string>::value是一个常量,用于检查类型T是否string类型一致
  • 然后在C++17中,你需要使用std::is_same_v<T, std::string>来检查类型;std::is_same_v<T, std::string>是一个宏,它会返回一个布尔值,表示类型T是否与std::string类型相同
  • std::enable_if<std::is_same<T, Json::Value>::value, bool>::type是一个模板类型,它会根据类型T是否与Json::Value类型相同来返回一个bool类型。
  • std::is_same_v的返回值是布尔值,而std::enable_if的返回值是一个模板类型。
  • std::is_same_v主要用于类型检查,而std::enable_if主要用于根据条件返回不同的类型。
cpp 复制代码
class Config
{
	friend class Singleton<Config>;
public:
	Config(){}
	~Config(){}
	
	bool ReadConfig(const std::string config_path)
	{
		std::cout << config_path  << "---" << config_path.empty() << std::endl;
		if (config_path.empty())
		{
			std::cout << "congfig file does not exist." << std::endl;
			return false;
		}
		std::ifstream input(config_path);
		input >> config_info_;
		input.close();
		return true;
	}
#if 1
	// c++14 
	template<typename T>
	typename std::enable_if<std::is_same<T, int>::value, bool>::type
	GetConfig(std::string key_str, T& value)
	{
        bool ret = false;
		if (config_info_.empty() || config_info_[key_str].isNull())
		{
			LOG(ERROR) << "get config value failure, key is :" << key_str;
			return false;
		}
	    if (config_info_[key_str].isConvertibleTo(Json::intValue))
	    {
	        value = config_info_[key_str].asInt();
            ret =true;;
	    }
	    LOG(INFO) << "config " << key_str << " is :" << value;
	    return ret;
	}

	template<typename T>
	typename std::enable_if<std::is_same<T, std::string>::value, bool>::type
	GetConfig(std::string key_str, T& value)
	{
        bool ret = false;
		if (config_info_.empty() || config_info_[key_str].isNull())
		{
			LOG(ERROR) << "get config value failure, key is :" << key_str;
			return false;
		}
	    if (config_info_[key_str].isConvertibleTo(Json::stringValue))
	    {
	        value = config_info_[key_str].asString();
            ret =true;;
	    }
	    LOG(INFO) << "config " << key_str << " is :" << value;
	    return ret;
	}

	template<typename T>
	typename std::enable_if<std::is_same<T, double>::value, bool>::type
	GetConfig(std::string key_str, T& value)
	{
        bool ret = false;
		if (config_info_.empty() || config_info_[key_str].isNull())
		{
			LOG(ERROR) << "get config value failure, key is :" << key_str;
			return false;
		}
	    if (config_info_[key_str].isConvertibleTo(Json::realValue))
	    {
	        value = config_info_[key_str].asDouble();
            ret =true;;
	    }
	    LOG(INFO) << "config " << key_str << " is :" << value;
	    return ret; 
	}

	template<typename T>
	typename std::enable_if<std::is_same<T, Json::Value>::value, bool>::type
	GetConfig(std::string key_str, T& value)
	{
		if (config_info_.empty() || config_info_[key_str].isNull())
		{
			LOG(ERROR) << "get config value failure, key is :" << key_str;
			return false;
		}
	    value = config_info_[key_str];
	    LOG(INFO) << "config " << key_str << " is :" << value;
	    return true;
	}
#else
	template <typename T>
	bool GetConfig(const std::string key_str, T& value)
	{
		if (config_info_.empty() || config_info_[key_str].isNull())
		{
			LOG(ERROR) << "get config value failure, key is :" << key_str;
			return false;
		}

		// if (std::is_same<T, int>::value)
		// {
		// 	value = config_info_[key_str].asInt();
		// }
		// else if (std::is_same<T, std::string>::value) {
		// 	value = config_info_[key_str].asString();
		// }
		// else if (std::is_same<T, double>::value) {
		// 	value = config_info_[key_str].asDouble();
		// }
		// else if (std::is_same<T, Json::Value>::value) {
		// 	value = config_info_[key_str];
		// }
		//else {
		//	LOG(ERROR) << "get config value failure, The parameter type is incorrect :" << typeid(T).name();
		//	return false;
		//}

		if constexpr (std::is_same_v<T, int>)
		{
			value = config_info_[key_str].asInt();
		}
		else if constexpr (std::is_same_v<T, std::string>) {
			value = config_info_[key_str].asString();
		}
		else if constexpr (std::is_same_v<T, double>) {
			value = config_info_[key_str].asDouble();
		}
		else if constexpr (std::is_same_v<T, Json::Value>) {
			value = config_info_[key_str];
		}
		else {
			LOG(ERROR) << "get config value failure, The parameter type is incorrect :" << typeid(T).name();
			return false;
		}
		LOG(INFO) << "config " << key_str << " is :" << value;
		return true;
	}
#endif

private:
	Json::Value config_info_;
};

typedef Singleton<Config> Configurator;
相关推荐
Dream it possible!1 小时前
LeetCode 热题 100_在排序数组中查找元素的第一个和最后一个位置(65_34_中等_C++)(二分查找)(一次二分查找+挨个搜索;两次二分查找)
c++·算法·leetcode
柠石榴1 小时前
【练习】【回溯No.1】力扣 77. 组合
c++·算法·leetcode·回溯
王老师青少年编程1 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
澄澈天空3 小时前
C++ MFC添加RichEditControl控件后,程序启动失败
c++·mfc
Lzc7744 小时前
C++初阶——简单实现vector
c++·简单实现vector
一个小白14 小时前
C++——list模拟实现
开发语言·c++
程序员老舅5 小时前
C++ Qt项目教程:WebServer网络测试工具
c++·qt·测试工具·webserver·qt项目·qt项目实战
靡不有初1115 小时前
CCF-CSP第18次认证第一题——报数【两个与string相关的函数的使用】
c++·学习·ccfcsp
cookies_s_s6 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
不想编程小谭7 小时前
力扣LeetCode: 2506 统计相似字符串对的数目
c++·算法·leetcode