打印多种不同类型的数字限制
Print numeric limits for multiple, varying types
在下面的代码中,我定义了一个名为 my_type
的unsigned int
,我用它来打印类型本身的最大值:
#include <iostream>
#include <limits>
#include <cmath>
...
using namespace std;
int main() {
typedef unsigned int my_type;
const my_type max_int = numeric_limits<my_type>::max():
cout << max_int << endl;
return 0;
}
如何对多种类型执行相同的操作而不必复制此代码?
我尝试创建一个字符串数组来存储unsigned int
和long
类型(例如),但这不起作用:
string current_type[2] = {"unsigned int", "long"};
loop{
typedef current_type[0..1] my_type;
const my_type max_int = numeric_limits<my_type>::max();
}
我也尝试过使用模板,但不知道该怎么做。
这可能吗?
#include <iostream>
#include <limits>
using namespace std;
template <typename T>
void printMax()
{
cout << numeric_limits<T>::max() << endl;
}
int main()
{
printMax<unsigned int>();
printMax<double>();
return 0;
}
和:
$ g++ test.cpp && ./a.out
4294967295
1.79769e+308
C++没有反射,因此无法将 C++ 字符串转换为类型名称。但是,您可以使用可变参数模板来完成任务。
#include <iostream>
#include <limits>
#include <cmath>
using namespace std;
template <typename ... Args>
struct TL;
template <typename T>
struct TL<T>
{
static void print()
{
const T max_int = numeric_limits<T>::max();
cout << max_int << endl;
}
};
template <typename T, typename ... Args>
struct TL<T, Args...>
{
static void print()
{
TL<T>::print();
TL<Args...>::print();
}
};
int main(int , char** )
{
TL<int, unsigned int, short int>::print();
return 0;
}
更新
更复杂的例子。
您可以声明用于保存类型列表的可变参数模板:
template <typename ... Args>
struct TypeList;
template <typename T>
struct TypeList<T>
{
typedef T type;
};
template <typename T, typename ... Args>
struct TypeList<T, Args...>
{
typedef T type;
// typedef TypeList<Args...> rest;
};
和模板来执行操作,取决于类型列表中每个元素的类型:
template <typename L, template <typename T> class Op>
struct ForEach;
template <typename T, template <typename T> class Op>
struct ForEach<TypeList<T>, Op>
{
void operator()()
{
Op<T>()();
}
};
template <typename T, template <typename T> class Op, typename ... Args>
struct ForEach<TypeList<T, Args...>, Op>
{
void operator()()
{
Op<T>()();
ForEach<TypeList<Args...> , Op>()();
}
};
现在,您可以使用 operator() 声明一些函数,例如模板化结构
#include <iostream>
#include <limits>
#include <cmath>
using namespace std;
template <typename T>
struct PrintNumericTypeMaxLimit
{
void operator()()
{
const T max_int = numeric_limits<T>::max();
cout << max_int << endl;
}
};
template <typename T>
struct PrintNumericTypeMinLimit
{
void operator()()
{
const T min = numeric_limits<T>::min();
cout << min << endl;
}
};
并将其与您的类型列表一起使用:
int main(int , char** )
{
typedef TypeList<int, unsigned int, long int, short int, unsigned short int, double> myList;
ForEach<myList, PrintNumericTypeMaxLimit>()();
ForEach<myList, PrintNumericTypeMinLimit>()();
return 0;
}
您可以使用
boost::variant
来通知所需的类型,boost::mpl::foreach
遍历它们以及一个函子,用于以 C++11 之前的方式或使用 C++11 lambda 打印数字限制
#include <iostream>
#include <limits>
#include <boost/variant.hpp>
#include <boost/mpl/for_each.hpp>
struct printMaxNumLimits
{
template<class Type>
void operator()(Type t) {
std::cout << std::numeric_limits<Type>::max() << std::endl;
}
};
int main()
{
using variant_types = boost::variant<int, double, unsigned int>;
// pre c++11
boost::mpl::for_each<variant_types::types>(printMaxNumLimits());
// c++11
boost::mpl::for_each<variant_types::types>([](auto t){
std::cout << std::numeric_limits<decltype(t)>::max() << std::endl;
});
}
现场示例
相关文章:
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 当我输入字符类型的数字时,为什么我无法获得整数?
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 你能在 c++ 中将不同的数字类型加在一起吗?
- 输出一个数字,该数字可能是三种类型之一
- 如何用不同的参数类型和数字回调函数
- C/C++ - 用于按顺序打印数字的 sem_t 类型的单个信号量
- typeid.name 返回派生类类型之前的数字
- C++小数点后有 200 位数字的类型?
- 为什么我不能在同一行中定义两个相同类型的类的成员指针
- 常量整数变量和数字的不同类型推导
- 检测<T>某些非数字类型 T 的 std::numeric::type 的特化
- 随机数字双重类型
- CRTP 模式 但是在数据结构中存储非同构类型
- 当数字可能超出C++中特定数据类型的范围时如何处理异常?
- 为什么数字类型只有"to_string()"?
- 如何在数字大于类型 size_t 的最大数量时使用 std::bitset
- C++从文件中读取不同类型的数据,直到有一个以数字开头的字符串
- 创建一个带有限制的数字数据类型,一个介于 -10 和 10 之间的整数
- 确定参数的数字和类型,以及返回函数类型参数的类型