gpt4 book ai didi

c++: 'std::is_fundamental' 的替代方案?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:07:50 25 4
gpt4 key购买 nike

在模板类中的函数中,我试图区分基本类型和其他类型。

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com