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;
相关推荐
AlexMercer10121 小时前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
小灰灰爱代码1 小时前
C++——求3个数中最大的数(分别考虑整数、双精度数、长整数的情况),用函数模板来实现。
开发语言·c++·算法
BeyondESH3 小时前
Linux线程同步—竞态条件和互斥锁(C语言)
linux·服务器·c++
豆浩宇3 小时前
Halcon OCR检测 免训练版
c++·人工智能·opencv·算法·计算机视觉·ocr
WG_173 小时前
C++多态
开发语言·c++·面试
Charles Ray5 小时前
C++学习笔记 —— 内存分配 new
c++·笔记·学习
重生之我在20年代敲代码5 小时前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
迷迭所归处10 小时前
C++ —— 关于vector
开发语言·c++·算法
CV工程师小林11 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
white__ice12 小时前
2024.9.19
c++