C++ 打印多种不同类型的数字限制
在以下代码中,我定义了一个名为C++ 打印多种不同类型的数字限制,c++,templates,typedef,C++,Templates,Typedef,在以下代码中,我定义了一个名为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>
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;
}
我也尝试过使用模板,但不知道如何使用
这是可能的吗?< /P> < P> C++没有反射,所以不能将C++字符串转换为类型名称。但是你可以使用可变模板来完成你的任务
#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;
}
#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;
}
#包括
#包括
#包括
使用名称空间std;
样板
结构TL;
样板
结构TL
{
静态无效打印()
{
常量T max_int=数值限制::max();
CUTC++没有反射,所以不能将C++字符串转换为类型名称,但是可以使用VixEdio模板来完成任务。
#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;
}
#包括
#包括
#包括
使用名称空间std;
样板
结构TL;
样板
结构TL
{
静态无效打印()
{
常量T max_int=数值限制::max();
cout您可以使用它来通知所需的类型,并循环它们
以及一个函子,用于以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;
});
}
#包括
#包括
#包括
#包括
结构printmaxnumlits
{
样板
void运算符()(类型t){
std::cout您可以使用它来通知所需的类型,并循环它们
以及一个函子,用于以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;
});
}
#包括
#包括
#包括
#包括
结构printmaxnumlits
{
样板
void运算符()(类型t){
std::我可以偷偷地怀疑这在C++的类型系统中是不可能的,但我不太确定我是否愿意回答这个问题。像Scala这样更复杂的类型系统会允许这样做,或者像C#这样带有反射的语言也可以。一种可能的解决方案可能是使用代码生成来处理所有重复的代码或者你。为什么不template void printLimits(){cout要让它处理字符串,你需要字符串和相应类型之间的映射。模板以类型作为参数,而不是字符串。恐怕你误解了我。我的意思是“如果你真的想使用字符串,你必须…”,但实际上,我强烈建议您不要使用Strings。我有一种潜在的怀疑,认为这在C++的类型系统中是不可能的,但我不太确定我是否愿意回答这个问题。像Scala这样更复杂的类型系统会允许这样做,或者像C#这样带有反射的语言也可以工作。一种可能的解决方案可能是使用代码生成为您处理所有复制。为什么不template void printLimits(){cout要使其处理字符串,您需要字符串和相应类型之间的映射。模板将类型作为参数,而不是字符串。ups,恐怕您误解了我。我的意思是“如果你真的想使用字符串,你必须。。。",但事实上,我强烈建议你不要使用Strings。如果不是因为tobi303在上面的评论中首先提出了这种模式,我会给你一张赞成票;-)@Rook这不是重点。没关系,谁发布了答案,如果这是正确的解决方案,你应该投赞成票并接受它。事实上,我只看到了一点他的评论我在按“提交”。@tobi303:还有“回答”不应该是没有解释的代码转储。@PreferenceBean我同意,但是如果我写了答案,我也不知道该解释什么/如何解释,因为OP已经提到了一个模板解决方案,但不清楚他的问题在哪里/是什么。实际上,我会更批评使用名称空间std;
的用法,我会给你一个UPF投票或者,如果不是因为tobi303在上面的评论中首先提出了这种模式;-)@Rook这不是重点。谁发布了答案并不重要,如果这是正确的解决方案,你应该投票并接受它。事实上。我在按下“提交”时看到了他的评论。@tobi303:还有一个“答案”不应该是没有解释的代码转储。@PreferenceBean我同意,但是如果我已经写了答案,我也不知道该解释什么/如何解释,因为OP已经提到了一个模板解决方案,但不清楚他的问题在哪里/是什么。实际上,我会更加批评使用名称空间std;
这正是我所知道的正如查找!虽然您不能使用反射来(比如)获取所有数字类型的列表,但您可以使用tt;string n=typeid(t).name()
来获取类型的(实现定义的)名称。然后您可以使用相同的类型列表或其他可变模板迭代机制来匹配用户定义的字符串(比如“i”)使用内部类型(int
,在我本地的gcc副本上)。这可能有点不可移植和讨厌,但它确实有效。这正是我所寻找的!虽然不能使用反射来获得所有数字类型的列表,但可以使用t;string n=typeid(t).name()
来获得(定义的实现)类型的名称。然后,您可以使用相同的类型列表或其他可变模板迭代机制将用户定义的字符串(比如“i”)与内部类型(在我的本地gcc副本上为int
)相匹配。这可能有点不可移植和讨厌,但它确实有效。
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>()();
}
};
#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;
}
#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;
});
}