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;
相关推荐
蒋星熠3 分钟前
C++零拷贝网络编程实战:从理论到生产环境的性能优化之路
网络·c++·人工智能·深度学习·性能优化·系统架构
CHANG_THE_WORLD16 分钟前
# C++ 中的 `string_view` 和 `span`:现代安全视图指南
开发语言·c++
雨落倾城夏未凉17 分钟前
9.c++new申请二维数组
c++·后端
雨落倾城夏未凉1 小时前
8.被free回收的内存是立即返还给操作系统吗?为什么?
c++·后端
雨落倾城夏未凉1 小时前
6.new和malloc的区别
c++·后端
郝学胜-神的一滴1 小时前
深入理解QFlags:Qt中的位标志管理工具
开发语言·c++·qt·程序人生
INS_KF2 小时前
【C++知识杂记2】free和delete区别
c++·笔记·学习
一只鱼^_2 小时前
牛客周赛 Round 105
数据结构·c++·算法·均值算法·逻辑回归·动态规划·启发式算法
ikkkkkkkl2 小时前
C++设计模式:面向对象设计原则
c++·设计模式·面向对象
啊阿狸不会拉杆2 小时前
《算法导论》第 27 章 - 多线程算法
java·jvm·c++·算法·图论