我无法让它工作。我希望它检查基本类型,但也检查基本类型的指针:
template<typename T> struct non_void_fundamental :
boost::integral_constant<bool,
(boost::is_fundamental<T>::value && !boost::is_same<T, void>::value)
|| (boost::is_fundamental<*T>::value && !boost::is_same<*T, void>::value)
>
{ };
也许有人可以帮我指出正确的方向。
编辑:尤其是第四行没有达到我想要的效果,其余的都可以正常工作。
编辑 2:重点是它在以下示例中生成以下输出:
int* p = new int(23);
cout << non_void_fundamental<double>::value << endl // true
cout << non_void_fundamental<some_class>::value << endl // false
cout << non_void_fundamental<p>::value << endl // true
编辑 3:感谢 Kerrek SB,我知道了这一点,但它产生了一些错误。
template<typename T> struct non_void_fundamental :
boost::integral_constant<bool,
(boost::is_fundamental<T>::value && !boost::is_same<T, void>::value)
|| (boost::is_pointer<T>::value && boost::is_fundamental<boost::remove_pointer<T>::type>::value && !boost::is_same<boost::remove_pointer<T>::type, void>::value)
>
{ };
错误:
FILE:99:61: error: type/value mismatch at argument 1 in temp
late parameter list for 'template<class T> struct boost::is_fundamental'
FILE:99:61: error: expected a type, got 'boost::remove_poi
nter<T>::type'
FILE:99:125: error: type/value mismatch at argument 1 in tem
plate parameter list for 'template<class T, class U> struct boost::is_same'
FILE:99:125: error: expected a type, got 'boost::remove_po
inter<T>::type'
最佳答案
你完全搞错了。让我们只关注“T
是一个指向基本类型的指针”:即:
T
是一个指针,并且
现在把它们放在一起。在伪代码中:
value = (is_fundamental<T>::value && !is_void<T>::value) ||
(is_pointer<T>::value && is_fundamental<remove_pointer<T>::type>::value)
在实际代码中,Boost版本:
#include <boost/type_traits.hpp>
template <typename T>
struct my_fundamental
{
static bool const value =
(boost::is_fundamental<T>::value && ! boost::is_void<T>::value) ||
(boost::is_pointer<T>::value &&
boost::is_fundamental<typename boost::remove_pointer<T>::type>::value);
};
在 C++11 中,将包含更改为 <type_traits>
和boost::
至std::
.
关于c++ - Boost::type_traits 基本类型和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13331585/