在模板类中的函数中,我试图区分基本类型和其他类型。
在 C++ 11 中你可以这样做:
if(std::is_fundamental<T>::value)
{
// Treat it as a primitive
}
else
{
//Treat it otherwise
}
如果我错了,请纠正我,这不仅在 C++ 11 中。
在早期版本的 c++ 中是否有替代方案?
最佳答案
你可以使用 Boost's type traits在 C++03 中是这样的:
#include <boost/type_traits/is_fundamental.hpp>
...
if(boost::is_fundamental<T>::value)
{
// Treat it as a primitive
}
else
{
//Treat it otherwise
}
我想这也适用于 C++98。
关于c++: 'std::is_fundamental' 的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16807414/