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;
相关推荐
丁劲犇31 分钟前
用 Turbo Vision 2 为 Qt 6 控制台应用创建 TUI 字符 MainFrame
开发语言·c++·qt·tui·字符界面·curse
charlie1145141911 小时前
深入理解Qt的SetWindowsFlags函数
开发语言·c++·qt·原理分析
呜喵王阿尔萨斯1 小时前
编程中的英语
c语言·c++
whoarethenext2 小时前
使用 C++/Faiss 加速海量 MFCC 特征的相似性搜索
开发语言·c++·faiss
向阳@向远方4 小时前
第二章 简单程序设计
开发语言·c++·算法
Mr_Xuhhh4 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
liulilittle4 小时前
VGW 虚拟网关用户手册 (PPP PRIVATE NETWORK 基础设施)
开发语言·网络·c++·网关·智能路由器·路由器·通信
ruanjiananquan995 小时前
c,c++语言的栈内存、堆内存及任意读写内存
java·c语言·c++
持梦远方6 小时前
C 语言基础入门:基本数据类型与运算符详解
c语言·开发语言·c++
江理不变情6 小时前
图像质量对比感悟
c++·人工智能