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主要用于根据条件返回不同的类型。
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
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