#pragma once
#include <string>
#include <type_traits>
class type_parser {
private:
std::string str_;
public:
type_parser(std::string str) : str_(str) {
}
template <typename T>
operator T() && {
void *tmp = nullptr;
if (std::is_integral<T>::value) {
auto t = atoi(str_.data());
tmp = &t;
}
else if (std::is_same<T, float>::value || std::is_same<T, double>::value) {
auto t = atof(str_.data());
tmp = &t;
}
else {
tmp = &str_;
}
return *((T *)(tmp));
}
};
// int a = type_parser("1111")
// double b = type_parser("23.333")
// std::string c = type_parser("ABC")
欺骗的艺术--骗过编译器,C++类型转换器
最新推荐文章于 2025-06-13 19:04:42 发布