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;
相关推荐
m0_5522008231 分钟前
《UE5_C++多人TPS完整教程》学习笔记37 ——《P38 变量复制(Variable Replication)》
c++·游戏·ue5
小wanga35 分钟前
【递归、搜索与回溯】专题三 穷举vs暴搜vs回溯vs剪枝
c++·算法·机器学习·剪枝
Code_流苏2 小时前
C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)
开发语言·c++·stl容器·课设·期末大作业·日历程序·面向对象设计
liulun3 小时前
Skia如何绘制几何图形
c++·windows
old_power3 小时前
UCRT 和 MSVC 的区别(Windows 平台上 C/C++ 开发相关)
c语言·c++·windows
獨枭3 小时前
彻底解决 MFC 自绘控件闪烁
c++·mfc
獨枭3 小时前
MFC Resource.h 文件详解与修改指南
c++·mfc
鑫鑫向栄3 小时前
[蓝桥杯]堆的计数
数据结构·c++·算法·蓝桥杯·动态规划
温宇飞3 小时前
C++ 匿名命名空间 vs static
c++
温宇飞4 小时前
C++ 泛型编程
c++